Пример #1
0
 public void Close()
 {
     if (_client == null)
     {
         return;
     }
     try
     {
         /*if (_client.ClientCredentials != null)
          * {
          *  _client.ClientCredentials.UserName.UserName = "******";
          *  _client.ClientCredentials.UserName.Password = "******".Reverse().ToString();
          * }*/
         _client.UnsubscribeFromPasswordChange();
     }
     catch (Exception exception)
     {
         _client?.Abort();
         _client = null;
         Logger?.LogTextMessage($"UnsubscribeFromPasswordChange() - error: {exception.Message}");
         return;
     }
     try
     {
         _client?.Close();
     }
     catch (Exception)
     {
     }
     _client = null;
 }
Пример #2
0
        public void Initialize()
        {
            Logger?.LogTextMessage("Initialize()...");
            _needToAddFaultHandlers = true;
            bool needSaveToLog = true;

            while (!Config.TaskCancellationToken.Token.IsCancellationRequested)
            {
                if (_client == null)
                {
                    try
                    {
                        _callbackInstanceContext = new InstanceContext(this);
                        var clientTcpBinding = new NetTcpBinding();
                        clientTcpBinding.Security.Mode = SecurityMode.TransportWithMessageCredential;
                        clientTcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

                        clientTcpBinding.OpenTimeout                       = TimeSpan.FromSeconds(10);
                        clientTcpBinding.ReceiveTimeout                    = TimeSpan.FromSeconds(10);
                        clientTcpBinding.ReliableSession.Enabled           = true;
                        clientTcpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromSeconds(10);

                        var clientEndpointAddress = new EndpointAddress(new Uri("net.tcp://" + Config.Ip + ":" + Config.Port + "/D2SecService"),
                                                                        new DnsEndpointIdentity("OICD2 Server"));

                        _client = new D2UserClient(_callbackInstanceContext, clientTcpBinding, clientEndpointAddress);

                        if (_client.ClientCredentials != null)
                        {
                            _client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
                            _client.ClientCredentials.UserName.UserName = "******";
                            _client.ClientCredentials.UserName.Password = new string("D2_User".ToCharArray().Reverse().ToArray());
                        }
                    }
                    catch (Exception exception)
                    {
                        if (needSaveToLog)
                        {
                            Logger?.LogTextMessage($"CreateClient() error: {exception.Message}\nStack:\n{exception.StackTrace}");
                            needSaveToLog = false;
                        }
                        Thread.Sleep(100);
                        continue;
                    }
                }
                try
                {
                    //var sw = Stopwatch.StartNew();
                    _client.SubscribeOnPasswordChange();
                    //sw.Stop();
                    //Debug.Print(sw.Elapsed.ToString());
                    foreach (var chan in _callbackInstanceContext.OutgoingChannels)
                    {
                        chan.Faulted += OnChannelFaulted;
                        //chan.Closed += OnChannelFaulted;
                    }
                }
                catch (Exception exception)
                {
                    if (_client.State == CommunicationState.Faulted)
                    {
                        _client.Abort();
                        if (needSaveToLog)
                        {
                            Logger?.LogTextMessage($"SubscribeOnPasswordChange() error: {exception.Message}");
                            needSaveToLog = false;
                        }
                        _client = null;
                        Thread.Sleep(100);
                        continue;
                    }
                }
                Logger?.LogTextMessage("Initialize() - OK");
                break;
            }
            ProcessCancellationPending();
        }