C# WCF从客户端的IErrorHandler.ProviderDefault访问FaultException详细信息
我正在尝试为C# WCF从客户端的IErrorHandler.ProviderDefault访问FaultException详细信息,c#,wcf,error-handling,C#,Wcf,Error Handling,我正在尝试为WCF服务的FaultException附加附加信息,然后在客户端访问它。我使用的是IErrorHandlerproviddefault方法,因为我的服务公开了许多方法,我想在一个地方定义错误处理逻辑 我已经定义了一个数据类CoreFault: [DataContract] public class CoreFault { [DataMember] public string Foo { get; set; } } 并实现了IErrorHandler接口以返回强类型
WCF
服务的FaultException
附加附加信息,然后在客户端访问它。我使用的是IErrorHandler
providdefault
方法,因为我的服务公开了许多方法,我想在一个地方定义错误处理逻辑
我已经定义了一个数据类CoreFault
:
[DataContract]
public class CoreFault
{
[DataMember]
public string Foo { get; set; }
}
并实现了IErrorHandler
接口以返回强类型的FaultException
:
public class ErrorHandler : IErrorHandler
{
public void ProvideFault(Exception error, MessageVersion version, ref Message message)
{
var faultException = new FaultException<CoreFault>(
new CoreFault
{
Foo = "Bar"
},
"An error occurred",
null);
var fault = faultException.CreateMessageFault();
message = Message.CreateMessage(
version,
fault,
faultException.Action);
}
}
我在创建消息后中断了代码,它显示了以下XML:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header/>
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-GB">An error occurred</faultstring>
<detail>
<CoreFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/EmployerPortal.Models.Faults">
<Foo>Bar</Foo>
</CoreFault>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
如何将Foo
传递给客户端,以便客户端在处理异常时可以访问它
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header/>
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-GB">An error occurred</faultstring>
<detail>
<CoreFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/EmployerPortal.Models.Faults">
<Foo>Bar</Foo>
</CoreFault>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
try
{
// Call service here
...
}
catch (FaultException<CoreFault> e)
{
throw;
}
catch (FaultException e)
{
// This line is hit
throw;
}