WCF 对服务上的所有操作使用相同的 IParameterInspector

发布于 2024-10-01 12:36:01 字数 298 浏览 4 评论 0原文

我已经实现了一个自定义 IParameterInspector,并且我希望让它针对我的服务上的每个操作执行。

我的理解是,IParameterInspector 实现只能与 IOperationBehavior 实现一起使用,并且实习生 IOperationBehavior 实现只能用于使用属性。

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

I have implemented a custom IParameterInspector and I want to have it execute for every single operation on my service.

My understanding is that IParameterInspector implementations can only be used with IOperationBehavior implementations, and that intern IOperationBehavior implementation can only be used to decorate individual operations using an attribute.

Does anyone know if there is a way I can register my IParameterInspector at a service level so that it can execute for all operations in the service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-10-08 12:36:01

感谢这个以及随后的这个,我找到了我要找的东西。

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);
                }
            }
        }
    }
}

Thanks to this and subsequenbtly this, I found what I was looking for.

IParameterInspector does not need to be at the IOperationBehavior level. They can be at the IServiceBehavior level. In the service level ApplyDispatchBehavior method you need to loop through all its operations and assign the inspector behaviour.

My class in full...

[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);
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文