自定义故障异常导致客户端出现wcf超时
我遵循了关于如何创建自定义错误异常的相同示例(通过创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用故障契约通过 WCF 发送异常。
http://msdn.microsoft.com/en-us/library/ms733721.aspx
您可以创建自定义故障异常
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