Esempio n. 1
0
 public static EndpointAddress BuildEndpoint(EndpointInformation info, ServerConnectionInformation server, Type serviceContractType)
 {
     if (server.ConnectionString.Contains(":"))
     {
         return(new EndpointAddress("net.tcp://" + server.ConnectionString + "/" + serviceContractType.Name + "/"));
     }
     else
     {
         return(new EndpointAddress("net.tcp://" + server.ConnectionString + ":" + DefaultPort + "/" + serviceContractType.Name + "/"));
     }
 }
Esempio n. 2
0
        private void WorkerThread()
        {
            try
            {
                while (true)
                {
                    _resetEvent.Reset();
                    _reconnectEvent.Reset();

                    if (_factory == null)
                    {
                        EndpointAddress endpoint = EndpointInformation.BuildEndpoint(new EndpointInformation(), _serverInfo, typeof(T));
                        Binding         binding  = BindingInformation.BuildBinding(new BindingInformation(), _serverInfo);
                        if (_callback != null)
                        {
                            _factory = new DuplexChannelFactory <T>(_callback, binding, endpoint);
                        }
                        else
                        {
                            _factory = new ChannelFactory <T>(binding, endpoint);
                        }
                    }

                    if (_factory.State != CommunicationState.Opened)
                    {
                        try
                        {
                            _factory.Open();
                            Channel = _factory.CreateChannel();
                            Channel.Ping();
                            ((ICommunicationObject)Channel).Closed  += Channel_Closed;
                            ((ICommunicationObject)Channel).Faulted += Channel_Closed;
                            lock (_stateLock)
                            {
                                State = SubscriptionState.Connected;
                            }
                            if (Connected != null)
                            {
                                Connected(this);
                            }
                        }
                        catch (ThreadAbortException)
                        {
                        }
                        catch (Exception ex)
                        {
                            _lastException = ex;
                            OnDisconnect(ex);
                        }
                    }
                    else
                    {
                        _resetEvent.WaitOne(300000);
                        try
                        {
                            Channel.Ping();
                        }
                        catch (ThreadAbortException) { }
                        catch (Exception ex)
                        {
                            _lastException = ex;
                            OnDisconnect(ex);
                        }
                    }

                    switch (WaitHandle.WaitAny(new WaitHandle[] { _resetEvent, _reconnectEvent }, 15000))
                    {
                    case WaitHandle.WaitTimeout: break;

                    case 0: break;

                    case 1:
                    {
                        Cleanup();
                        lock (_stateLock)
                        {
                            State = SubscriptionState.Connecting;
                        }
                    }
                    break;
                    }
                }
            }
            finally
            {
                Cleanup();
            }
        }