[英]CommunicationException using IErrorHandler in WCF
i'm using IErrorHandler to implement a global error handler for my WCF service. 我正在使用IErrorHandler为WCF服务实现全局错误处理程序。 I've setup a dummy service that throws an exception to test, but I keep getting a CommunicationException instead of a FaultException (exception message is "The underlying connection was closed: The connection was closed unexpectedly.").
我已经设置了一个虚拟服务,该服务会抛出一个异常以进行测试,但是我不断收到CommunicationException而不是FaultException(异常消息是“基础连接已关闭:该连接意外关闭。”)。 Here's the relevant code:
以下是相关代码:
IErrorHandler implementation: IErrorHandler的实现:
public bool HandleError(Exception error)
{
return true;
}
public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
if (!(error is FaultException))
{
MessageFault msgFault = null;
FaultException<string> fex = new FaultException<string>("General Error");
msgFault = fex.CreateMessageFault();
fault = Message.CreateMessage(version, msgFault, string.Empty);
}
}
Any ideas? 有任何想法吗?
Thanks, Gonzalo 谢谢,贡萨洛
Try this: 尝试这个:
MessageFault msgFault = null;
FaultException<string> fex = new FaultException<string>("General Error");
msgFault = MessageFault.CreateFault(new FaultCode("Sender"), fex.Reason, fex, new NetDataContractSerializer());
fault = Message.CreateMessage(version, msgFault, string.Empty);
This works: 这有效:
FaultException newEx = new FaultException();
MessageFault msgFault = newEx.CreateMessageFault();
fault = Message.CreateMessage(version, msgFault, newEx.Action);
Found here: 在这里找到:
http://blogs.msdn.com/b/pedram/archive/2008/01/25/wcf-error-handling-and-some-best-practices.aspx http://blogs.msdn.com/b/pedram/archive/2008/01/25/wcf-error-handling-and-some-best-practices.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.