客户端 WCF 异常处理程序

3

使用BehaviorExtension在客户端处理类似TimeoutException、EndpointNotFoundException、OperationTimeoutException等错误是否可能?

我不想每次使用WCF代理时都要尝试捕获、记录、重新启动连接。

以下代码无法正常工作:

Behavyior:

public class EndpointBehavior:IEndpointBehavior
{
    public void Validate(ServiceEndpoint endpoint)
    {

    }

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {


    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        var handler = new WcfErrorHandler();
        clientRuntime.CallbackDispatchRuntime.ChannelDispatcher.ErrorHandlers.Add(handler);
    }
}

处理程序:

public class WcfErrorHandler:IErrorHandler
{
    public bool HandleError(Exception error)
    {
        Log.Instance.Trace(@"WCF Service Error:", error);
        return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        var newEx = new FaultException(
           string.Format(@"Exception caught at WcfErrorHandler{0} Method: {1}{2}Message:{3}",
                        Environment.NewLine, error.TargetSite.Name, Environment.NewLine, error.Message));

        var msgFault = newEx.CreateMessageFault();
        fault = Message.CreateMessage(version, msgFault, newEx.Action);
    }

}

扩展app.config配置文件

public class ExceptionLogBehaviorExtensionElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get 
        {
            return typeof(EndpointBehavior);
        }
    }

    protected override object CreateBehavior()
    {
        return new EndpointBehavior();
    }
}
1个回答

0

你的代码无法工作,因为IErrorHandler是仅限于服务器端的功能。

在你的代码中,看起来像是将错误处理程序添加到客户端请求中,但实际上并非如此。这是因为clientRuntime.CallbackDispatchRuntime是一种类似于服务器的实体,它存在于客户端并用于接收来自真实服务器的双向操作消息。

关于你的主要问题——客户端异常处理,我最好的建议是使用IClientMessageInspector。以下是一些代码,可以帮助你入门:

public sealed class LoggingEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(new LoggingInspector());
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }

    public void Validate(ServiceEndpoint endpoint) { }
}

public sealed class LoggingInspector : IClientMessageInspector
{
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        Console.WriteLine("BeforeSendRequest");
        return null;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        Console.WriteLine("AfterReceiveReply");
    }
}

然而,请注意IClientMessageInspector有一个缺点。它的AfterReceiveReply方法会在成功和失败的响应中被调用,但在完全没有响应时不会被调用,例如TimeoutException、EndpointNotFoundException等情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接