客户端WCF异常处理程序

本文关键字:异常处理程序 WCF 客户端 | 更新日期: 2023-09-27 18:25:58

是否可以使用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();
    }
}

客户端WCF异常处理程序

您的代码无法工作,因为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方法在来自服务器的成功和错误回复时调用,但当根本没有任何回复时,不调用,例如在TimeoutExceptionEndpointNotFoundException等上。