WCF客户端错误处理 [英] WCF client-side error-handling

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

问题描述

我消费,偶尔会抛出各种异常笨重的WCF服务器,另外一些返回的错误为字符串。我有服务器代码都不能访问。



我要重写内WCF客户端请求调用方法和处理由返回的所有内部异常和硬编码错误服务器和如果发生错误,提高故障事件伪:

 类MyClient:MyServiceSoapClient 
{
保护覆盖OnInvoke()
{
对象的结果;

{
结果= base.OnInvoke();
如果(结果==错误)
{
//筹集故障事件
}

{
//提高故障事件
}
}
}

所以,当我打电话 myClient.GetHelloWorld(),它会通过我重写的方法。



如何才能实现这一目标?结果
我知道我没有使用生成的客户端,但我不想再重新执行所有的合同,我想用生成的 ClientBase 子类,或至少它的渠道。结果
我需要的是在内部请求调用方法控制。



更新



我读到这回答,并期待它的部分我在寻找什么的,但我不知道是否有附加的 IErrorHandler 给消费者(客户)只有代码,我想将它添加到一个方法 ClientBase< TChannel方式> 实例莫名其妙



更新



文章看起来也非常有前途,但它不工作。应用属性似乎并没有生效。
我无法找到一个方法来添加 IServiceBehavior接口到客户端。



更新



我试图附加一个 IErrorHandler 通过 IEndpointBehavior.ApplyClientBehavior 调用:

 公共无效ApplyClientBehavior(ServiceEndpoint终点,clientRuntime clientRuntime)
{
clientRuntime.CallbackDispatchRuntime.ChannelDispatcher.ErrorHandlers
。新增(新的ErrorHandler());
}



clientRuntime MyErrorHandler 参数),但例外仍然抛出直接跳过。结果
ApplyDispatchBehavior 不是。叫在所有



结论



我需要实现两个方面的内容:




  1. 包装所有的 BaseClient<寿命期间可能发生的异常; TChannel> 和决定是否处理它们或扔上。这应该采取一切操作照顾(我消耗了服务公开数十)

  2. 解析所有服务器的响应,并抛出异常的一些人,所以他们转发在声明1。


解决方案

我已经结束了使用基于的这个问题



它坚持生成的客户端代码,并允许调用操作一般。



代码 是不完整的,随意叉子和编辑。请通知我,如果你发现任何错误或作出任何更新



这是非常庞大的,所以我只会共享使用代码:

 使用(VAR代理=新ClientProxy< MyServiceSoapClientChannel,MyServiceSoapChannel>())
{
client.Exception + =(发件人,EventArgs)= >
{
//所有的异常都会来到这里,可以通过重写ClientProxy定制。
Console.WriteLine($ @A'{eventArgs.Exception.GetType()}'发生操作过程中{} eventArgs.Operation.Method.Name'
);
eventArgs.Handled = TRUE;
};
client.Invoke(client.Client.MyOperation,ARG1,ARG2);
}


I'm consuming a clunky WCF server that occasionally throws various exceptions, and additionally returns some of its errors as string. I have no access to the server code at all.

I want to override the inner WCF-client request invocation method and handle all inner exceptions and hard-coded errors returned by the server and raise the Fault event if an error occurs, pseudo:

class MyClient : MyServiceSoapClient
{
    protected override OnInvoke()
    {
        object result;
        try
        {
            result = base.OnInvoke();
            if(result == "Error")
            {
                //raise fault event
            }
        catch
        {
            //raise fault event
        }
    }        
}

So that when I call myClient.GetHelloWorld(), it goes thru my overridden method.

How can this be achieved?
I know I don't have to use the generated client, but I don't want to re-implement all the contracts again, and I want to use the generated ClientBase subclass or at least its channel.
What I need is control over the inner request call method.

Update

I read this answer, and looks it's partially what I'm looking for, but I'm wondering if there is a way to attach an IErrorHandler to the consumer (client) code only, I want to add it to the ClientBase<TChannel> instance somehow.

Update

This article also looks very promising but it doesn't work. The applied attribute doesn't seem to take effect. I can't find a way to add IServiceBehavior to the client side.

Update

I tried attaching an IErrorHandler via IEndpointBehavior.ApplyClientBehavior calling:

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
  clientRuntime.CallbackDispatchRuntime.ChannelDispatcher.ErrorHandlers
           .Add(new ErrorHandler());
}

(clientRuntime is a parameter), but exceptions are still thrown directly skipping MyErrorHandler.
ApplyDispatchBehavior isn't called at all.

Conclusion

I need to achieve two aspects:

  1. Wrap all exceptions that might occur during the lifetime of a BaseClient<TChannel> and decide whether to handle them or throw them on. This should take care of all operation (the service I'm consuming exposes few dozens)
  2. Parse all server-replies and throw exceptions for some of them, so they're forwarded as in statement 1.

解决方案

I've ended up using something based on the answers in this question.

It sticks to the generated client code, and allows invocation of the operations generically.

The code is incomplete, feel free to fork and edit it. Please notify me if you found any bugs or made any updates.

It's pretty bulky so I'll just share the usage code:

using (var proxy = new ClientProxy<MyServiceSoapClientChannel, MyServiceSoapChannel>())
{
  client.Exception += (sender, eventArgs) =>
  {
    //All the exceptions will get here, can be customized by overriding ClientProxy.
    Console.WriteLine($@"A '{eventArgs.Exception.GetType()}' occurred 
      during operation '{eventArgs.Operation.Method.Name}'.");
    eventArgs.Handled = true;
  };
  client.Invoke(client.Client.MyOperation, "arg1", "arg2");
}

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

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