WCF 错误处理

发布于 2024-11-14 04:31:06 字数 2555 浏览 7 评论 0原文

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

我正在使用自托管 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; }
}

Q How can I get the original exception (occurred on the server) on the client side?

I am working with a self-hosted WCF service and C# 4 and trying to set up proper exception handling.

I have a client that looks like this

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

And service implementation

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

Service web.config has

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

This is my original exception
enter image description here

And this is what I get on the cliententer image description here

If needed I can post full details on the client side, however the exception on the client does not have any reference to the original one.

Update
This is my interface with defined 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);
}

And this is my WcfException class

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光病人 2024-11-21 04:31:06

您是否尝试过捕获FaultException?

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

您需要在合同中包含 FaultContractAttribute这个工作。

Have you tried to catch a FaultException instead?

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

You will need to include a FaultContractAttribute in your contract for this to work.

小情绪 2024-11-21 04:31:06

什么是 WcfException?它是否可序列化?

无论如何,我建议您按照这篇 MSDN 文章<中所述在服务器上启用跟踪/a> - 这应该可以帮助您诊断问题。

What is WcfException and is it serializable?

In any case, I suggest you enable tracing on the server as described in this MSDN article - this should help you diagnose the problem.

悲喜皆因你 2024-11-21 04:31:06

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

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

If you want to have information about original exception it is enough to use:

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>
夏了南城 2024-11-21 04:31:06

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

Thanks for all your answers. My problem was in the service interface, namely in the Action property of FaultContractAttribute, it pointed to the incorrect 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);
}

I solved it by removing all other properties (to be determined automatically) and left only the type of 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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文