【问题标题】:WCF fault handlingWCF 错误处理
【发布时间】:2011-09-09 04:08:00
【问题描述】:

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

服务 web.config 有

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

这是我原来的例外

这就是我在客户端得到的

如果需要,我可以在客户端发布完整的详细信息,但是客户端上的异常与原始异常没有任何参考。

更新
这是我定义了 FaultContractAttribute

的界面
[ServiceContract]
public interface IServiceOperation : IOperation
{
    ...
    [OperationContract(Action = "http://tempuri.org/ITestRunnerService/VerifyAction", ReplyAction = "http://tempuri.org/ITestRunnerService/VerifyAction")]
    [FaultContractAttribute(typeof(WcfException), Action = "http://tempuri.org/ITestRunnerService/ExecuteRequestWcfExceptionFault", Name = "WcfException", Namespace = "http://schemas.datacontract.org/2004/07/TH.Core.Exceptions")]
    ServiceResponse VerifyAction(ServiceRequest request);
}

这是我的 WcfException

[DataContract]
public class WcfException : Exception
{
    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public new string Message { get; set; }

    [DataMember]
    public new string InnerException { get; set; }

    [DataMember]
    public new string StackTrace { get; set; }
}

【问题讨论】:

    标签: c# wcf exception-handling wcf-faults


    【解决方案1】:

    您是否尝试过捕获 FaultException?

    catch (FaultException<WcfException> ex)
    {
       AppManager.LogException(ex);
    }
    catch (FaultException unknownFault)
    {
       AppManager.LogException(unknownFault);
    }
    

    您需要在合同中包含FaultContractAttribute 才能使用。

    【讨论】:

    • tnx 的回复,它不起作用。无论如何,FaultException 是异常,如果我捕捉到子异常或父异常,它不会有太大变化。我不想有冗长的捕获块,这对我的情况来说是可以的。
    • @oleksii:您是否也在您的操作中添加了 FaultContractAttribute?
    • @Christia.K 它最初在那里,我会尝试查看跟踪信息。
    【解决方案2】:

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

    无论如何,我建议您按照this MSDN article 中的说明在服务器上启用跟踪 - 这应该可以帮助您诊断问题。

    【讨论】:

    • 是的,它是可序列化的。我现在将检查跟踪。
    【解决方案3】:

    如果您想获得有关原始异常的信息,使用它就足够了:

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

    【讨论】:

      【解决方案4】:

      感谢您的所有回答。我的问题出在服务接口,即FaultContractAttributeAction属性中,它指向了错误的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);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多