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

C# Net.tcp服务方法和异常处理-WCF,c#,.net,wcf,tcp,proxy,C#,.net,Wcf,Tcp,Proxy,我有一个带有服务方法的WCF服务,它是在自定义代理的帮助下使用net.tcp公开的。我能够使用自定义代理访问我的服务方法,没有任何问题。 但是,在我的一个服务方法中添加异常处理后,代理无法与服务建立连接 工作代码和非工作代码之间的唯一区别在于,非工作代码对a服务方法有适当的异常处理;其中工作版本没有异常处理 到目前为止我做了什么 public void Use(UseServiceDelegate<T> codeBlock, string wcfEndPoint) {

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

工作代码和非工作代码之间的唯一区别在于,非工作代码对a服务方法有适当的异常处理;其中工作版本没有异常处理

到目前为止我做了什么

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;
        }
  • 我确实试图将安全模式从无更改为消息 例如:
    binding.Security.Mode=SecurityMode.Message
  • 我重新启动了诸如
    Net.Tcp Listner适配器、Net.Tcp端口共享服务
    Net.Pipe Listner适配器
    等服务
  • 我确实在管理模式下执行了
    ServiceModelReg.exe-r
    命令
  • 服务合同

    [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;
            }
    

    在服务器和客户端同时启用WCF服务跟踪,这将使您了解问题所在。@Morbia,在服务器上启用了跟踪。但不确定客户端如何连接,因为连接是通过客户端的自定义proxy.WCF跟踪建立的,与端点无关,只需在client app.config中添加跟踪配置,它就可以工作了。