如何在回调方向扩展WCF?

发布于 2024-12-06 23:02:04 字数 196 浏览 0 评论 0原文

我可以使用行为将 IParameterInspector 附加到 ClientRuntime 中的每个操作以及服务端 DispatchRuntime 中的每个操作。但似乎这只适用于从客户端到服务。

我还希望能够在从服务到客户端的回调中附加一个 IParameterInspector,如上所述,但我找不到任何可扩展点来执行此操作。

有什么想法吗?

I can attach an IParameterInspector using behaviors to each operation in the ClientRuntime and also to each operation in the DispatchRuntime on the service side. But it seems this only works from client to service.

I also want to be able to attach an IParameterInspector in the callbacks from service to client on both sides of the wire as above but I can't find any extensibility points to do this.

Any ideas?

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

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

发布评论

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

评论(1

美人如玉 2024-12-13 23:02:04

这有点晦涩难懂,并且似乎没有详细记录,但您可以使用标准 WCF 行为功能来自定义两端。

在客户端,这个属性会让它发生。

public class InspectorBehaviorAttribute : Attribute, IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        foreach (var item in clientRuntime.CallbackDispatchRuntime.Operations)
        {
            item.ParameterInspectors.Add(ParameterInspector.Instance);
        }
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

只需将此属性应用于实现回调接口的类即可。

在服务器上,事情变得有点棘手。您需要通过 ApplyDispatchBehavior 进行连接。在本例中,我通过服务行为来完成此操作,但主体也适用于 OperationBehaviors 和 EndpointBehaviors。

public class InspectorBehaviorAttribute : Attribute, IServiceBehavior
{
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (var item in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>())
        {
            foreach (var ep in item.Endpoints)
            {
                foreach (var op in ep.DispatchRuntime.CallbackClientRuntime.Operations)
                {
                    op.ParameterInspectors.Add(ParameterInspector.Instance);
                }
            }
        }
    }

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

同样,只需将此属性应用于您的服务实现,即可将参数检查器用于所有回调操作。

虽然这些示例演示了连接 IParameterInspector 实现,但所有其他 WCF 扩展点的相同方法可用于在客户端和服务器上自定义回调通道。

This is a little obscure and does not appear to be all that well documented but you can customise both ends using standard WCF behavior capabilities.

On the client, this attribute would make it happen.

public class InspectorBehaviorAttribute : Attribute, IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        foreach (var item in clientRuntime.CallbackDispatchRuntime.Operations)
        {
            item.ParameterInspectors.Add(ParameterInspector.Instance);
        }
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

Simply apply this attribute on the class that implements your callback interface.

On the server, it gets a little trickier. You need to hook up via the ApplyDispatchBehavior. In this case I have done it through a service behavior but the principal applies to OperationBehaviors and EndpointBehaviors as well.

public class InspectorBehaviorAttribute : Attribute, IServiceBehavior
{
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (var item in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>())
        {
            foreach (var ep in item.Endpoints)
            {
                foreach (var op in ep.DispatchRuntime.CallbackClientRuntime.Operations)
                {
                    op.ParameterInspectors.Add(ParameterInspector.Instance);
                }
            }
        }
    }

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

Again, simply apply this attribute to your service implementation to have your parameter inspector utilised for all callback operations.

While these examples demonstrate hooking up IParameterInspector implementations, the same approach for all other WCF extension points can be used to customise callback channels at both the client and server.

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