【问题标题】:How to handle exceptions thrown in IParameterInspector?如何处理 IParameterInspector 中抛出的异常?
【发布时间】:2014-09-16 10:11:27
【问题描述】:

我找遍了整个地方,但似乎找不到答案。我有一个使用 IParameterInspector 的扩展端点行为。在 BeforeCall 方法中抛出异常时如何处理?

我尝试将 try-catch 添加到 IEndPointBehavior 和 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;
    }
}

【问题讨论】:

    标签: c# .net wcf endpointbehavior


    【解决方案1】:

    我终于设法解决了。我不得不像这样实现 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());
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2013-03-05
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多