2014-09-16 2 views
1

나는 이곳 저곳을 돌아 다니며 대답을 찾을 수없는 것 같습니다. IParameterInspector를 사용하는 확장 끝점 동작이 있습니다. BeforeCall 메서드에서 throw 될 때 예외를 처리 할 수 ​​있습니까?IParameterInspector에서 발생한 예외를 처리하는 방법은 무엇입니까?

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

답변

2

나는 마침내 그것을 해결하기 위해 관리 않았다. 그래서 같은 IErrorHandler를 구현했다 : ApplyDispatchBehavior이 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) 
    { 

    } 
} 

을 다음 추가

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