1

私はあちこち探し回っていますが、答えが見つからないようです。IParameterInspector を使用する拡張エンドポイントの動作があります。BeforeCall メソッドでスローされた例外を処理するにはどうすればよいですか?

IEndPointBehavior と BehaviorExtensionElement に try-catch を追加しようとしましたが、どちらもそれを処理しません。ここにいくつかのコードがあります:

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

1 に答える 1

2

私は最終的にそれを解決することができました。次のように 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());
    }
于 2014-09-16T15:00:00.137 に答える