簡體   English   中英

如何處理IParameterInspector中引發的異常?

[英]How to handle exceptions thrown in IParameterInspector?

我一直在到處尋找,似乎找不到答案。 我有一個使用IParameterInspector的擴展端點行為。 在BeforeCall方法中引發異常時,該如何處理?

我嘗試將try-catch添加到IEndPointBehavior和BehaviorExtensionElement中,但兩者均無法處理。 這是一些代碼:

BehaviorExtensionElement:

public class ExtensionService : BehaviorExtensionElement
{
    protected override object CreateBehavior()
    {
        //try-catch doesn't work here
        return new ExtensionBehavior();

    }

    public override Type BehaviorType
    {
        get { return typeof(ExtensionBehavior); }
    }
}

IEndpointBehavior:

public class ExtensionBehavior : IEndpointBehavior
{

    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
        //throw new NotImplementedException();
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        foreach (ClientOperation clientOperation in clientRuntime.ClientOperations)
        {
            //try-catch here doesn't work
            clientOperation.ClientParameterInspectors.Add(new ParamInspector());

        }
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        foreach (DispatchOperation dispatchOperation in endpointDispatcher.DispatchRuntime.Operations)
        {
                //try-catch here doesn't work
                dispatchOperation.ParameterInspectors.Add(new ParamInspector());
        }
    }

    public void Validate(ServiceEndpoint endpoint)
    {
        //throw new NotImplementedException();
    }
}

IParameterInspector

public class ParamInspector : IParameterInspector
{

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {

    }

    public object BeforeCall(string operationName, object[] inputs)
    {
        ///an exception is thrown here
        return null;
    }
}

我終於設法解決了。 我必須像這樣實現IErrorHandler:

public class CustomErrorHandler : IErrorHandler
{

    public bool HandleError(Exception error)
    {
        //the procedure for handling the errors.
        //False is returned because every time we have an exception we want to abort the session.
        return false;
    }

    public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
    {

    }
}

然后將此IErrorHandler添加到ApplyDispatchBehavior

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        foreach (DispatchOperation dispatchOperation in endpointDispatcher.DispatchRuntime.Operations)
        { 
                dispatchOperation.ParameterInspectors.Add(new ParamInspector(this.Class));
        }
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new CustomErrorHandler());
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM