WCF故障处理 [英] WCF fault handling

查看:102
本文介绍了WCF故障处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:我怎样才能得到原始异常在客户端(发生在服务器上)?



我与自我工作-hosted WCF服务和C#4,试图建立适当的异常处理。



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

 私人ServiceResponse PerformRemoteAction(ServiceRequest要求,ProcessOperations操作)
{
...

{
/ /远程调用
开关(操作)
{
...
情况下ProcessOperations.VerifyAction:{响应= client.VerifyAction(请求);打破; }

默认:抛出新NotImplementedException();
}
}
赶上(异常前)
{
AppManager.LogException(除息);
}

返回响应;
}

和服务实现

 公众覆盖ServiceResponse VerifyAction(ServiceRequest要求)
{
RegisterRequest(请求);


{
变种链=新VerificationChain();
=性反应chain.Run(请求);
}
赶上(异常前)
{
抛出新的FaultException< WcfException>(ExceptionFactory.Create(前),
新FaultReason(ex.Message));
}

SetSuccessfulResponce();

的回报性反应;
}

服务web.config中有



 <&行为GT; 
< serviceBehaviors>
<&行为GT;
< serviceDebug includeExceptionDetailInFaults =真/>
< /行为>
< / serviceBehaviors>
< /行为>

这是我原来的异常



这就是我得到的客户端上的



如果需要,我可以在客户端发布的全部细节,但客户端上的例外,不具有原来的任何引用。



更新

这是我的接口与定义的 FaultContractAttribute

  [的ServiceContract] 
公共接口IServiceOperation:一个IOperation
{
...
[OperationContract的(动作=http://tempuri.org/ITestRunnerService/VerifyAction,ReplyAction =http://tempuri.org/ITestRunnerService/VerifyAction)]
[FaultContractAttribute(typeof运算(WcfException),行动=http://tempuri.org/ITestRunnerService/ExecuteRequestWcfExceptionFaultNAME =WcfException,命名空间=http://schemas.datacontract.org/2004/07/TH .Core.Exceptions)]
ServiceResponse VerifyAction(ServiceRequest要求);
}



这是我的 WcfException 的类

  [DataContract] 
公共类WcfException:异常
{
[数据成员]
公字符串名称{搞定;组; }

[数据成员]
公开新字符串消息{搞定;组; }

[数据成员]
公开新的字符串的InnerException {搞定;组; }

[数据成员]
公开新的字符串堆栈跟踪{搞定;组; }
}


解决方案

感谢您的答案。我的问题是在服务接口,即在动作的财产的 FaultContractAttribute 的,它指的不正确的URL。

  [的ServiceContract] 
公共接口IServiceOperation:一个IOperation
{
...
[OperationContract的(动作=HTTP:// tempuri .ORG / IServiceOperation / VerifyAction,ReplyAction =http://tempuri.org/IServiceOperation/VerifyAction)]
[FaultContractAttribute(typeof运算(WcfException),行动=http://tempuri.org/IServiceOperation/ ExecuteRequestWcfExceptionFaultNAME =WcfException,命名空间=http://schemas.datacontract.org/2004/07/TH.Core.Exceptions)]
ServiceResponse VerifyAction(ServiceRequest要求);
}



我解决它通过去除所有其他属性(自动确定)和左只有WcfFault类型

  [的ServiceContract] 
公共接口IServiceOperation:一个IOperation
{
...
[OperationContract的(动作=http://tempuri.org/IServiceOperation/VerifyAction,ReplyAction =http://tempuri.org/IServiceOperation/VerifyAction)]
[FaultContractAttribute( typeof运算(WcfException))]
ServiceResponse VerifyAction(ServiceRequest要求);
}


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

And this is what I get on the client

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

解决方案

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

这篇关于WCF故障处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆