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

时间:2011-03-30 12:55:43

标签: c# wcf behavior inspector

我有一个自定义行为,我在其中实现“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;
    }
}

EDIT 如果我的验证失败,那么中止通话的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

据我所知,抛出异常是唯一可以中止的方式。