c# – 使用IParameterInspector.BeforeCall(string operationName,object [] inputs)中止一个调用

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

这是我的属性:

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

编辑
如果我的验证失败,最好的中止通话方式是什么?

最佳答案 抛出异常是唯一的方法 – 据我所知 – 中止.

点赞