使用IParameterInspector.BeforeCall(字符串operationName,object 输入)中止调用

我有一个自定义行为,我实现“IParameterInspector”,以便能够使用BeforeCall和AfterCall。 我正在使用这些方法在我的WCF服务上执行调用之前进行一些validation。

这是我的属性:

[AttributeUsage(AttributeTargets.Class)] public class SendReceiveBehaviorAttribute : Attribute, IServiceBehavior { public SendReceiveBehaviorAttribute() { } public void ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host) { foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers) { foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints) { foreach (DispatchOperation op in eDispatcher.DispatchRuntime.Operations) { op.ParameterInspectors.Add(MyInspector); } } } } public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection endpoints, BindingParameterCollection bindingParameters) { } public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } } 

我的检查员:

 internal class MyInspector: IParameterInspector { public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) { } public object BeforeCall(string operationName, object[] inputs) { // Make some verifications and cancel if it fails. return null; } } 

编辑如果validation失败,中止通话的最佳方法是什么?

抛出exception是唯一的方法 – 据我所知 – 中止。