Net.tcp服务方法和异常处理 - WCF

时间:2014-03-17 11:14:51

标签: c# .net wcf tcp proxy

我有一个带有服务方法的WCF服务,该服务方法在自定义代理的帮助下使用net.tcp公开。我能够毫无困难地使用自定义代理访问我的服务方法。 但是,在我的一个服务方法中添加异常处理后,代理无法与服务建立连接。

工作代码与非工作代码之间的唯一区别在于,非工作代码具有适用于服务方法的异常处理;工作工作版本没有异常处理。

到目前为止我做了什么

  1. 我确实尝试将安全模式从None更改为Message 例如:binding.Security.Mode = SecurityMode.Message;
  2. 我重新启动了Net.Tcp Listner adaptor,Net.Tcp Port Sharing serviceNet.Pipe Listner Adaptor等服务。
  3. 我在管理模式下执行了ServiceModelReg.exe -r命令。
  4. 服务合同

    [ServiceContract(Namespace = "http://mydom.com/mainrt/subrt")]
    public interface IService
    {
     [OperationContract]
     [FaultContract(typeof(ServiceException))]
     string CallToServiceMethod(object dataTransferObject);
    }
    
    [DataContract(Namespace = "http://mydom.com/mainrt/subrt")]
    public class ServiceException
     {
            [DataMember]
            public string ServiceMethodName { get; set; }
    
            [DataMember]
            public string ErrorMessage { get; set; }
    
            [DataMember]
            public string ErrorInnerException { get; set; }
    
            public ServiceException()
            {
                ServiceMethodName = string.Empty;
                ErrorMessage = string.Empty;
                ErrorInnerException = string.Empty;
            }
        }
    

    实施 - 不工作

    public string CallToServiceMethod(object dataTransferObject)
            {
                try
                {
                   //code goes here               
                }
                catch (Exception ex)
                {
                    throw new FaultException<ServiceException>(new ServiceException()
                                                                        {
                                                                            ErrorInnerException = ex.InnerException.Message,
                                                                            ErrorMessage = ex.Message,
                                                                            ServiceMethodName = "CallToServiceMethod"
                                                                        });
    }
    

    实施 - 工作

    public string CallToServiceMethod(object dataTransferObject)
                {
                    try
                    {
                       //code goes here               
                    }
                    catch (Exception ex)
                    {
                        // Log exception details                
                    }
              }
    

    自定义代理和触发器

    public void Use(UseServiceDelegate<T> codeBlock, string wcfEndPoint)
            {
                try
                {
                    this.proxy = GetChannelFactory(wcfEndPoint).CreateChannel() as IClientChannel;
                    if (this.proxy != null)
                    {
                        this.proxy.Open(); // Here it fails when I have the exception handling. 
                        codeBlock((T)this.proxy);
                        this.proxy.Close();
                    }
                }
                catch (CommunicationException commEx)
                {
                    if (this.proxy != null)
                        this.proxy.Abort();
    
                    throw commEx;
                }
    
            }
    
     ChannelFactory<T> GetChannelFactory(string wcfEndPoint)
            {
                ChannelFactory<T> channelFactory = null;
    
                if (!channelPool.TryGetValue(wcfEndPoint, out channelFactory))
                {
                    NetTcpBinding binding = new NetTcpBinding();
                    binding.MaxReceivedMessageSize = 20000000;
                    binding.ReaderQuotas.MaxStringContentLength = 20000000;
                    binding.ReaderQuotas.MaxBytesPerRead = 20000000;
    
                    binding.Security.Mode = SecurityMode.None; // I did check it from None to Message/TCP but no luck
                    binding.Security.Transport.ClientCredentialType = cpClientCredentialType.Windows;
                    binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
                    binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
    
    
                    EndpointIdentity epIdentity = EndpointIdentity.CreateUpnIdentity("email@email.come");
                    EndpointAddress epAddress = new EndpointAddress(new Uri(wcfEndPoint), epIdentity, new AddressHeaderCollection());
    
                    channelFactory = new ChannelFactory<T>(binding, epAddress);
                    channelPool.Add(wcfEndPoint, channelFactory);
                }
    
                return channelFactory;
            }
    

    异常消息(当我在代理对象上调用Open方法时)

    {System.ServiceModel.EndpointNotFoundException: The message could not be dispatched because the 
     service at the endpoint address 'net.tcp://xxxxxxxxx/Service.svc' is unavailable for the protocol of the address.
    
    Server stack trace: 
       at System.ServiceModel.Channels.ConnectionUpgradeHelper.DecodeFramingFault(ClientFramingDecoder decoder, IConnection connection, Uri via, String contentType, TimeoutHelper& timeoutHelper)
       at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection, ArraySegment`1 preamble, TimeoutHelper& timeoutHelper)
       at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(IConnection connection, TimeoutHelper& timeoutHelper)
       at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
       at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open()
    

0 个答案:

没有答案