C# WCF故障处理

C# WCF故障处理,c#,wcf,exception-handling,wcf-faults,C#,Wcf,Exception Handling,Wcf Faults,Q如何在客户端获取原始异常(发生在服务器上) 我正在使用自托管WCF服务和C#4,并尝试设置适当的异常处理 我有一个客户看起来像这样 private ServiceResponse PerformRemoteAction(ServiceRequest request, ProcessOperations operation) { ... try { //remote call switch (operation) {

Q如何在客户端获取原始异常(发生在服务器上)

我正在使用自托管WCF服务和C#4,并尝试设置适当的异常处理

我有一个客户看起来像这样

private ServiceResponse PerformRemoteAction(ServiceRequest request, ProcessOperations operation)
{
    ...    
    try
    {
        //remote call
        switch (operation)
        {
            ...
            case ProcessOperations.VerifyAction: { response = client.VerifyAction(request); break; } 

            default: throw new NotImplementedException();
        }
    }
    catch (Exception ex)
    {
        AppManager.LogException(ex);
    }

    return response;
}
和服务实现

public override ServiceResponse VerifyAction(ServiceRequest request)
{
    RegisterRequest(request);

    try
    {
        var chain = new VerificationChain();
        Responce = chain.Run(request);
    }
    catch (Exception ex)
    {
        throw new FaultException<WcfException>(ExceptionFactory.Create(ex),
            new FaultReason(ex.Message));
    }

    SetSuccessfulResponce();

    return Responce;
}
这是我的WCFEException类


您是否尝试捕获FaultException

catch (FaultException<WcfException> ex)
{
   AppManager.LogException(ex);
}
catch (FaultException unknownFault)
{
   AppManager.LogException(unknownFault);
}
catch(FaultException-ex)
{
AppManager.LogException(ex);
}
捕获(FaultException unknownFault)
{
AppManager.LogException(未知故障);
}

您需要在合同中包含一个,才能使其生效。

什么是
WcfException
,它可以序列化吗


在任何情况下,我建议您在服务器上启用跟踪,如中所述-这将有助于您诊断问题。

如果您想获得有关原始异常的信息,可以使用:

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>


谢谢您的回答。我的问题是在服务接口中,即在FaultContractAttribute的Action属性中,它指向了错误的URL

[ServiceContract]
public interface IServiceOperation : IOperation
{
    ...
    [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
    [FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/IServiceOperation/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
    ServiceResponse VerifyAction(ServiceRequest request);
}
我通过删除所有其他属性(自动确定)解决了这个问题,只保留了WcfFault的类型

[ServiceContract]
    public interface IServiceOperation : IOperation
    {
        ...
        [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
        [FaultContractAttribute(typeof(WcfException))]
        ServiceResponse VerifyAction(ServiceRequest request);
    }

tnx的答复是,它不起作用。无论如何,FaultException是一个异常,如果我捕捉到子异常或父异常,它不会有太大的变化。我不想有太长的捕获块,这适合我的情况。@oleksii:你也在你的操作中添加了FaultContractAttribute吗?@Christia.K它最初在那里,我会尝试查看跟踪信息。是的,它是可序列化的。我现在要查一下痕迹。
<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>
[ServiceContract]
public interface IServiceOperation : IOperation
{
    ...
    [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
    [FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/IServiceOperation/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
    ServiceResponse VerifyAction(ServiceRequest request);
}
[ServiceContract]
    public interface IServiceOperation : IOperation
    {
        ...
        [OperationContract(Action = "http://tempuri.org/IServiceOperation/VerifyAction", ReplyAction = "http://tempuri.org/IServiceOperation/VerifyAction")]
        [FaultContractAttribute(typeof(WcfException))]
        ServiceResponse VerifyAction(ServiceRequest request);
    }