WCF与服务上的所有操作相同的IParameterInspector

And*_*age 13 wcf extensibility

我已经实现了一个自定义IParameterInspector,我想让它为我的服务上的每一个操作执行.

我的理解是IParameterInspector实现只能用于IOperationBehavior实现,而实习IOperationBehavior实现只能用于使用属性来装饰各个操作.

有谁知道我是否可以IParameterInspector在服务级别注册我的方式,以便它可以执行服务中的所有操作?

And*_*age 14

多亏了这个,并且后来这个,我找到了我想要的东西.

IParameterInspector不需要在这个IOperationBehavior级别.他们可以在这个IServiceBehavior级别.在服务级别 ApplyDispatchBehavior方法中,您需要遍历其所有操作并分配检查器行为.

全班我的课......

[AttributeUsage(AttributeTargets.Class)]
public class ServiceLevelParameterInspectorAttribute : Attribute, IParameterInspector, IServiceBehavior
{
    public object BeforeCall(string operationName, object[] inputs)
    {
       // Inspect the parameters.
        return null;
    }

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
        {
            if (channelDispatcher == null)
            {
                continue;
            }

            foreach(var endPoint in channelDispatcher.Endpoints)
            {
                if (endPoint == null)
                {
                    continue;
                }

                foreach(var opertaion in endPoint.DispatchRuntime.Operations)
                {
                    opertaion.ParameterInspectors.Add(this);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 有点旧,但为什么继续调用你的代码? (2认同)