4

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

编辑如果我的验证失败,终止我的通话的最佳方法是什么?

4

1 回答 1

3

抛出异常是唯一的方法——据我所知——中止。

于 2011-03-30T13:07:08.243 回答