Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何处理IParameterInspector中引发的异常?_C#_.net_Wcf_Endpointbehavior - Fatal编程技术网

C# 如何处理IParameterInspector中引发的异常?

C# 如何处理IParameterInspector中引发的异常?,c#,.net,wcf,endpointbehavior,C#,.net,Wcf,Endpointbehavior,我到处找了,似乎找不到答案。我有一个使用IParameterInspector的扩展端点行为。在BeforeCall方法中抛出异常时,如何处理该异常 我尝试将try-catch添加到IEndPointBehavior和BehaviorExtensionElement中,这两个元素都不能处理它。下面是一些代码: BehaviorExtensionElement: public class ExtensionService : BehaviorExtensionElement { prote

我到处找了,似乎找不到答案。我有一个使用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); }
    }
}
IENDPoint行为:

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();
    }
}
i参数检测器

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());
    }