Пример #1
0
        /// <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));
            }
        }