예제 #1
0
        /// <summary>
        /// Connects to backend windows service.
        /// </summary>
        public static void ConnectWindowsServiceClient(bool overwrite = false)
        {
            TcpClient remotingClient;
            ISecurityProvider provider;
            UserData userData;

            if (overwrite)
            {
                DisconnectWindowsServiceClient();
                ServiceConnectionRefreshed(null, EventArgs.Empty);
            }
            else
            {
                if (s_windowsServiceClient == null || s_windowsServiceClient.Helper.RemotingClient.CurrentState != TVA.Communication.ClientState.Connected)
                {
                    if (s_windowsServiceClient != null)
                        DisconnectWindowsServiceClient();

                    AdoDataConnection database = new AdoDataConnection(DefaultSettingsCategory);
                    try
                    {
                        string connectionString = database.ServiceConnectionString(true);   //.RemoteStatusServerConnectionString();

                        if (!string.IsNullOrWhiteSpace(connectionString))
                        {
                            s_windowsServiceClient = new WindowsServiceClient(connectionString);

                            if (SecurityProviderCache.TryGetCachedProvider(CurrentUser, out provider))
                            {
                                userData = provider.UserData;

                                if ((object)userData != null)
                                {
                                    s_windowsServiceClient.Helper.Username = userData.LoginID;
                                    s_windowsServiceClient.Helper.Password = SecurityProviderUtility.EncryptPassword(provider.Password);
                                    remotingClient = s_windowsServiceClient.Helper.RemotingClient as TcpClient;

                                    if ((object)remotingClient != null && (object)provider.SecurePassword != null && provider.SecurePassword.Length > 0)
                                        remotingClient.NetworkCredential = new NetworkCredential(userData.LoginID, provider.SecurePassword);
                                }

                                s_windowsServiceClient.Helper.RemotingClient.MaxConnectionAttempts = -1;
                                s_windowsServiceClient.Helper.RemotingClient.ConnectionEstablished += RemotingClient_ConnectionEstablished;
                                ThreadPool.QueueUserWorkItem(ConnectAsync, null);
                            }
                        }
                    }
                    finally
                    {
                        if (database != null)
                            database.Dispose();
                    }
                }
            }
        }