Exemplo n.º 1
0
        private WmsClient GetClient()
        {
            // если клиент уже открыть, то вернем сразу
            if (!_switchEndpoint && _cln != null && _cln.State == CommunicationState.Opened)
            {
                return(_cln);
            }

            lock (_clnLock)
            {
                var lastSdcl = WMSEnvironment.Instance.SdclCode;
                if (_switchEndpoint || (_cln == null || _cln.State == CommunicationState.Faulted || _cln.State == CommunicationState.Closed ||
                                        _cln.State == CommunicationState.Closing))
                {
                    Disconnect();
                    var binding  = new NetTcpBinding("NetTcpBinding");
                    var endPoint = GetEndpoint();
                    _cln = new WmsClient(binding, endPoint);
                    //_cln.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 0);
                    _cln.ChannelFactory.Closed  += ChannelFactoryOnClosed;
                    _cln.ChannelFactory.Faulted += ChannelFactoryOnFaulted;
                    _cln.ChannelFactory.Opened  += ChannelFactoryOnOpened;
                    _cln.InnerChannel.Faulted   += InnerChannelOnFaulted;
                    _cln.InnerChannel.Closed    += InnerChannelOnClosed;
                    _cln.InnerChannel.Opened    += InnerChannelOnOpened;
                    _cln.InnerChannel.UnknownMessageReceived += InnerChannel_UnknownMessageReceived;
                    _log.DebugFormat("Create new communication client");
                }

                // пока ждали лок, могли уже открыть сессию
                if (_cln.State == CommunicationState.Opened)
                {
                    return(_cln);
                }

                // стратуем сессию
                _sId = _cln.StartSession(ClientType, WMSEnvironment.Instance.SessionId);

                _log.DebugFormat("Session '{0}' started", _sId);

                // обновим сессию, если был разрыв канала
                if (WMSEnvironment.Instance.SessionId.HasValue && WMSEnvironment.Instance.SdclCode != lastSdcl)
                {
                    var clientProvider = IoC.Instance.Resolve <ISessionRegistrator>();
                    clientProvider.UpdateCurrentSession(WMSEnvironment.Instance.SdclCode);
                }
                return(_cln);
            }
        }
Exemplo n.º 2
0
        private void Disconnect()
        {
            try
            {
                if (_cln == null)
                {
                    return;
                }

                if (_cln.State == CommunicationState.Opened)
                {
                    var sessionId = _cln.TerminateSession();
                    if (_sId.EqIgnoreCase(sessionId))
                    {
                        _log.DebugFormat("Terminate session id '{0}'", sessionId);
                    }
                    else
                    {
                        _log.DebugFormat("Terminate session id '{0}' but expected '{1}'", sessionId, _sId);
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Debug(ex);
            }
            finally
            {
                if (_cln != null)
                {
                    _cln.Abort();
                    _cln.ChannelFactory.Closed  -= ChannelFactoryOnClosed;
                    _cln.ChannelFactory.Faulted -= ChannelFactoryOnFaulted;
                    _cln.ChannelFactory.Opened  -= ChannelFactoryOnOpened;
                    _cln.InnerChannel.Faulted   -= InnerChannelOnFaulted;
                    _cln.InnerChannel.Closed    -= InnerChannelOnClosed;
                    _cln.InnerChannel.Opened    -= InnerChannelOnOpened;
                    _cln.InnerChannel.UnknownMessageReceived -= InnerChannel_UnknownMessageReceived;
                }
                _cln = null;
            }
        }