自定义故障异常导致客户端出现wcf超时

发布于 2024-10-28 09:19:02 字数 2572 浏览 4 评论 0原文

我遵循了关于如何创建自定义错误异常的相同示例(通过创建一个 Exception 类并在FaultContract 中引用它),但是每次我尝试从服务器抛出异常时,我都会收到来自客户端的连接超时,并且连接最接近。 奇怪的是,如果我使用默认的FaultException类从服务器抛出错误,那么客户端会正确接收它。

这是一个片段:

[DataContract(Namespace = "http://MyApp.Common", Name = "WcfException")]
public class WcfException
{
    private string methodName;
    private string nameSpace;
    private string errorDescription;
    private string stackTrace;
    private System.Exception exCeption;

    public WcfException()
    {
        methodName = String.Empty;
        nameSpace = String.Empty;
        errorDescription = String.Empty;
        stackTrace = String.Empty;
        exCeption = null;
    }

    [DataMember]
    public System.Exception MethodException
    {
        get
        {
            return exCeption;
        }
        set
        {
            exCeption = value;
            errorDescription = exCeption.Message;
            stackTrace = exCeption.StackTrace;
        }
    } // and so on....

我像这样初始化服务器:

                Type serviceType = typeof(Proxy);
                Uri baseUri = new Uri(@"net.tcp://" + Environment.MachineName + ":8030/");
                NetTcpBinding Binding = new NetTcpBinding();
                serviceHost = new ServiceHost(serviceType, baseUri);

                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                serviceHost.Description.Behaviors.Add(behavior);

                ServiceDebugBehavior sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
                sdb.IncludeExceptionDetailInFaults = true;

                Binding.Name = "ConfigurationHttpChannel";
                Binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                Binding.Security.Mode = SecurityMode.None;
                Binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
                Binding.SendTimeout = new TimeSpan(0, 10, 0);
                Binding.OpenTimeout = new TimeSpan(0, 10, 0);
                Binding.CloseTimeout = new TimeSpan(0, 10, 0);
                Binding.MaxReceivedMessageSize = 65536;
                Binding.MaxBufferSize = 65536;
                Binding.MaxBufferPoolSize = 524288;
                Binding.TransferMode = TransferMode.Buffered;

                serviceHost.AddServiceEndpoint(typeof(IProxy), Binding, "MyApi");
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), Binding, "mex");

                serviceHost.Open();

知道为什么只有当我使用自定义异常时才会出现连接超时吗?

非常感谢。

I followed the same example on how to create a custom fault exception (by creating an Exception class and referencing it in the FaultContract), however every time I try to throw the exception from the server, I receive a connection timeout from the client and the connection is closest.
The weird thing, if I use the default FaultException class to throw an error from the server, then the client receive it correctly.

here's a snippet:

[DataContract(Namespace = "http://MyApp.Common", Name = "WcfException")]
public class WcfException
{
    private string methodName;
    private string nameSpace;
    private string errorDescription;
    private string stackTrace;
    private System.Exception exCeption;

    public WcfException()
    {
        methodName = String.Empty;
        nameSpace = String.Empty;
        errorDescription = String.Empty;
        stackTrace = String.Empty;
        exCeption = null;
    }

    [DataMember]
    public System.Exception MethodException
    {
        get
        {
            return exCeption;
        }
        set
        {
            exCeption = value;
            errorDescription = exCeption.Message;
            stackTrace = exCeption.StackTrace;
        }
    } // and so on....

I initialize the server like this:

                Type serviceType = typeof(Proxy);
                Uri baseUri = new Uri(@"net.tcp://" + Environment.MachineName + ":8030/");
                NetTcpBinding Binding = new NetTcpBinding();
                serviceHost = new ServiceHost(serviceType, baseUri);

                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                serviceHost.Description.Behaviors.Add(behavior);

                ServiceDebugBehavior sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
                sdb.IncludeExceptionDetailInFaults = true;

                Binding.Name = "ConfigurationHttpChannel";
                Binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                Binding.Security.Mode = SecurityMode.None;
                Binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
                Binding.SendTimeout = new TimeSpan(0, 10, 0);
                Binding.OpenTimeout = new TimeSpan(0, 10, 0);
                Binding.CloseTimeout = new TimeSpan(0, 10, 0);
                Binding.MaxReceivedMessageSize = 65536;
                Binding.MaxBufferSize = 65536;
                Binding.MaxBufferPoolSize = 524288;
                Binding.TransferMode = TransferMode.Buffered;

                serviceHost.AddServiceEndpoint(typeof(IProxy), Binding, "MyApi");
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), Binding, "mex");

                serviceHost.Open();

Any idea why I am getting a connection timeout Only when i use the custom exception?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2024-11-04 09:19:02

您应该使用故障契约通过 WCF 发送异常。

http://msdn.microsoft.com/en-us/library/ms733721.aspx

您可以创建自定义故障异常

[OperationContract]
[FaultContract(typeof(MathFault))]
int Divide(int n1, int n2);

You should use a Fault Contract to send exceptions over WCF.

http://msdn.microsoft.com/en-us/library/ms733721.aspx

You can create custom fault exceptions

[OperationContract]
[FaultContract(typeof(MathFault))]
int Divide(int n1, int n2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文