Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF从客户端的IErrorHandler.ProviderDefault访问FaultException详细信息_C#_Wcf_Error Handling - Fatal编程技术网

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;
}