/// <summary> /// 发起订阅并注册身份 /// </summary> public void Subscribe() { NetTcpBinding binding = new NetTcpBinding(SecurityMode.None); binding.MaxBufferSize = int.MaxValue; binding.MaxReceivedMessageSize = int.MaxValue; binding.ReaderQuotas.MaxArrayLength = 65536; binding.ReaderQuotas.MaxBytesPerRead = 10 * 1024 * 1024; binding.ReaderQuotas.MaxStringContentLength = 10 * 1024 * 1024; //10M; binding.ReceiveTimeout = TimeSpan.MaxValue; //设置连接自动断开的空闲时长; binding.SendTimeout = new TimeSpan(0, 10, 9); _serviceProxy = DuplexChannelFactory <IMessagePublishService> .CreateChannel(_listener, binding, new EndpointAddress(_serviceUri)); //下面通过配置,仅供测试 //_serviceProxy = DuplexChannelFactory<IMessagePublishService>.CreateChannel(new InstanceContext(_listener), "defaultEndpoint"); try { string indentity = "PDF.NET;20111230;" + HardDiskSN.SerialNumber; _serviceProxy.QuikRegist(indentity); _registed = true; Closed = false; } catch (Exception ex) { OnErrorMessage(string.Format("Subscribe Error,ErrorMessage:{0}", ex.Message)); } }
public void Dispose() { cancel.Cancel(); channelFactory.Close(); proxy.Unregist(); (proxy as IDisposable).Dispose(); _listener = null; proxy = null; }
/// <summary> /// 发起订阅并注册身份 /// </summary> /// <param name="userName">订阅的用户名</param> /// <param name="password">订阅的用户密码</param> public void Subscribe(string userName, string password) { NetTcpBinding binding = new NetTcpBinding(SecurityMode.None); binding.MaxBufferSize = int.MaxValue; binding.MaxReceivedMessageSize = int.MaxValue; binding.ReaderQuotas.MaxArrayLength = 65536; binding.ReaderQuotas.MaxBytesPerRead = 10 * 1024 * 1024; binding.ReaderQuotas.MaxStringContentLength = 10 * 1024 * 1024; //10M; binding.ReceiveTimeout = TimeSpan.MaxValue; //设置连接自动断开的空闲时长; binding.SendTimeout = new TimeSpan(0, 10, 9); _serviceProxy = DuplexChannelFactory <IMessagePublishService> .CreateChannel(_listener, binding, new EndpointAddress(_serviceUri)); //下面通过配置,仅供测试 //_serviceProxy = DuplexChannelFactory<IMessagePublishService>.CreateChannel(new InstanceContext(_listener), "defaultEndpoint"); ((ICommunicationObject)_serviceProxy).Faulted += Subscriber_Faulted; try { if (string.IsNullOrEmpty(userName)) { userName = "******"; } if (string.IsNullOrEmpty(password)) { password = "******"; } _listener.UserName = userName; _listener.Password = password; if (string.IsNullOrEmpty(this.RegisterData)) { this.RegisterData = "REGDATA"; } string indentity = _listener.GetIdentity() + ":" + this.RegisterData; _serviceProxy.QuikRegist(indentity); _registed = true; Closed = false; this.UserName = userName; this.Password = password; } catch (System.ServiceModel.EndpointNotFoundException ex1) { //服务地址错误,服务不可用 //已经触发基类 Faulted 事件 } catch (System.ServiceModel.ProtocolException ex2) { //服务协议错误 //已经触发基类 Faulted 事件 } catch (Exception ex) { OnErrorMessage(string.Format("Subscribe Error,ErrorMessage:{0}", ex.Message)); } }
static void Main(string[] args) { var serviceCollection = new ServiceCollection(); NativeDI.RegisterServices(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); _messagePublishService = serviceProvider.GetService <IMessagePublishService>(); Timer timer = new Timer(Callback, null, 0, 5000); Console.ReadLine(); }
private void innerDispose() { int count = 0; while (_waitCallbacking) { System.Threading.Thread.Sleep(100); count++; if (count > waitTimeOut) { Closed = true; OnErrorMessage("等待服务回调超时,innerDispose退出。Send Message:" + _currSendMessage); break; } } try { if (_serviceProxy != null) { if (this._closedFlag != 0) //关闭标志==0 表示在注册订阅阶段发生的事件,指示未注册成功 { _serviceProxy.Unregist(); } if (_registed) { (_serviceProxy as IDisposable).Dispose(); } } } catch (Exception ex) { OnErrorMessage(string.Format("innerDispose Error {0}", ex.Message)); } finally { } _listener = null; _serviceProxy = null; }
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(); }