我正在使用IErrorHandler为我的WCF服务实现全局错误处理程序。我设置了一个抛出异常进行测试的虚拟服务,但是我一直收到一个CommunicationException而不是FaultException(异常消息是"底层连接已关闭:连接意外关闭。" )。这是相关代码:
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);
}
}
有什么想法吗?
谢谢Gonzalo
答案 0 :(得分:0)
试试这个:
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);
答案 1 :(得分:0)
这有效:
FaultException newEx = new FaultException();
MessageFault msgFault = newEx.CreateMessageFault();
fault = Message.CreateMessage(version, msgFault, newEx.Action);
在此处找到:
http://blogs.msdn.com/b/pedram/archive/2008/01/25/wcf-error-handling-and-some-best-practices.aspx