4

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

4 回答 4

4

您是否尝试过捕获 FaultException?

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

您需要在合同中包含一个FaultContractAttribute才能使其正常工作。

于 2011-06-06T10:07:29.887 回答
3

什么是WcfException可序列化的?

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

于 2011-06-06T10:22:26.453 回答
2

如果您想获得有关原始异常的信息,则使用以下内容就足够了:

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>
于 2011-06-06T11:05:39.337 回答
1

感谢您的所有回答。我的问题出在服务接口中,即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);
    }
于 2011-06-06T12:45:19.670 回答