Esempio n. 1
0
        public void Subscribe()
        {
            cancel = new CancellationTokenSource();
            NetTcpBinding binding = new NetTcpBinding();
            binding.SendTimeout =new TimeSpan(0,0,30);
            EndpointAddress endpoint = new EndpointAddress(_serviceUri);
            //_serviceProxy = DuplexChannelFactory<IMessagePublishService>.CreateChannel(_listener, binding, endpoint);
            InstanceContext callback = new InstanceContext(_listener);
            channelFactory = new DuplexChannelFactory<IMessagePublishService>(callback, binding, endpoint);

            Task.Factory.StartNew(()=>
            {
                proxy = channelFactory.CreateChannel();
                try
                {
                    proxy.Regist();
                    IsConnected = true;
                }
                catch { }
                while (!cancel.Token.IsCancellationRequested)
                {
                    Thread.Sleep(3000);
                    try
                    {
                        proxy.IsConnected();
                        if (!IsConnected)
                        {
                            proxy.Regist();
                            IsConnected = true;
                        }
                    }
                    catch
                    {
                        IsConnected = false;
                        try
                        {
                            Console.WriteLine("正在重连");
                            proxy = channelFactory.CreateChannel();
                            proxy.Regist();
                            IsConnected = proxy.IsConnected();
                        }
                        catch
                        {
                            Console.WriteLine("重连异常");
                        }
                    }
                }
            }, cancel.Token);

            //_serviceProxy.Regist();
        }