WCF路由——如何以编程方式正确添加过滤表

发布于 2024-11-09 05:39:33 字数 340 浏览 0 评论 0原文

我正在使用 WCF 4 路由服务,并且需要以编程方式配置该服务(而不是通过配置)。我见过的这样做的例子很少见,创建一个 MessageFilterTable 如下:

            var filterTable=new MessageFilterTable<IEnumerable<ServiceEndpoint>>();

但是,该方法的通用参数应该是 TFilterData (您要过滤的数据类型)?我有自己的接受字符串的自定义过滤器 - 我仍然可以通过这种方式创建过滤器表吗?

如果这可行...路由基础设施是否会从我传入的列表中创建客户端端点?

I am using the WCF 4 routing service, and need to configure the service programmatically (as opposed to via config). The examples I have seen of doing so, which are rare, create a MessageFilterTable as follows:

            var filterTable=new MessageFilterTable<IEnumerable<ServiceEndpoint>>();

But, the generic parameter to that method is supposed to be TFilterData (the type of data you are filtering on)? I have my own custom filter that accepts a string -- can I still create the filter table this way?

If this will work...will the routing infrastructure create client endpoints out of the list I pass in?

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

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

发布评论

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

评论(2

旧竹 2024-11-16 05:39:33

我创建了一个 WCF 4 路由服务并以编程方式配置它。我的代码比它需要的间隔要大一些(其他人的可维护性是一个问题,因此有评论),但它确实有效。它有两个过滤器:第一个过滤到给定端点的一些特定操作,第二个将其余操作发送到通用端点。

        // Create the message filter table used for routing messages
        MessageFilterTable<IEnumerable<ServiceEndpoint>> filterTable = new MessageFilterTable<IEnumerable<ServiceEndpoint>>();

        // If we're processing a subscribe or unsubscribe, send to the subscription endpoint
        filterTable.Add(
            new ActionMessageFilter(
                "http://etcetcetc/ISubscription/Subscribe",
                "http://etcetcetc/ISubscription/KeepAlive",
                "http://etcetcetc/ISubscription/Unsubscribe"),
            new List<ServiceEndpoint>()
            {
                new ServiceEndpoint(
                    new ContractDescription("ISubscription", "http://etcetcetc/"),
                    binding,
                    new EndpointAddress(String.Format("{0}{1}{2}", TCPPrefix, HostName, SubscriptionSuffix)))
            },
            HighRoutingPriority);

        // Otherwise, send all other packets to the routing endpoint
        MatchAllMessageFilter filter = new MatchAllMessageFilter();
        filterTable.Add(
            filter,
            new List<ServiceEndpoint>()
            {
                new ServiceEndpoint(
                    new ContractDescription("IRouter", "http://etcetcetc/"),
                    binding,
                    new EndpointAddress(String.Format("{0}{1}{2}", TCPPrefix, HostName, RouterSuffix)))
            },
            LowRoutingPriority);

        // Then attach the filter table as part of a RoutingBehaviour to the host
        _routingHost.Description.Behaviors.Add(
            new RoutingBehavior(new RoutingConfiguration(filterTable, false)));

I have created a WCF 4 routing service and configured it programmatically. My code is a bit more spaced out than it needs to be (maintainability for others being a concern, hence the comments), but it definitely works. This has two filters: one filters some specific Actions to a given endpoint, and the second sends the remaining actions to a generic endpoint.

        // Create the message filter table used for routing messages
        MessageFilterTable<IEnumerable<ServiceEndpoint>> filterTable = new MessageFilterTable<IEnumerable<ServiceEndpoint>>();

        // If we're processing a subscribe or unsubscribe, send to the subscription endpoint
        filterTable.Add(
            new ActionMessageFilter(
                "http://etcetcetc/ISubscription/Subscribe",
                "http://etcetcetc/ISubscription/KeepAlive",
                "http://etcetcetc/ISubscription/Unsubscribe"),
            new List<ServiceEndpoint>()
            {
                new ServiceEndpoint(
                    new ContractDescription("ISubscription", "http://etcetcetc/"),
                    binding,
                    new EndpointAddress(String.Format("{0}{1}{2}", TCPPrefix, HostName, SubscriptionSuffix)))
            },
            HighRoutingPriority);

        // Otherwise, send all other packets to the routing endpoint
        MatchAllMessageFilter filter = new MatchAllMessageFilter();
        filterTable.Add(
            filter,
            new List<ServiceEndpoint>()
            {
                new ServiceEndpoint(
                    new ContractDescription("IRouter", "http://etcetcetc/"),
                    binding,
                    new EndpointAddress(String.Format("{0}{1}{2}", TCPPrefix, HostName, RouterSuffix)))
            },
            LowRoutingPriority);

        // Then attach the filter table as part of a RoutingBehaviour to the host
        _routingHost.Description.Behaviors.Add(
            new RoutingBehavior(new RoutingConfiguration(filterTable, false)));
時窥 2024-11-16 05:39:33

您可以在 MSDN 上找到一个很好的示例:如何:动态更新路由表

请注意,他们不直接创建 MessageFilterTable 的实例,而是使用新 RoutingConfiguration 实例提供的“FilterTable”属性。

如果您编写了自定义过滤器,那么您将像这样添加它:

rc.FilterTable.Add(new CustomMessageFilter("customStringParameter"), new List<ServiceEndpoint> { physicalServiceEndpoint });

CustomMessageFilter 将是您的过滤器,“customStringParameter”是(我相信)您正在谈论的字符串。
当路由器收到连接请求时,它将尝试通过此表条目映射它,如果成功,那么您是对的,路由器将创建一个客户端端点来与您提供的 ServiceEndpoint 进行通信。

You can find a good example on MSDN here: How To: Dynamic Update Routing Table

Note how they dont directly create an instance of the MessageFilterTable, but instead use the 'FilterTable' property provided by a new RoutingConfiguration instance.

If you have written a custom filter, then you will add it like this:

rc.FilterTable.Add(new CustomMessageFilter("customStringParameter"), new List<ServiceEndpoint> { physicalServiceEndpoint });

The CustomMessageFilter will be your filter, and the "customStringParameter" is the string that (I believe) you are talking about.
When the Router receives a connection request, it will attempt to map it via this table entry, if this is successful, then you are right, the router will create a client endpoint to talk to the ServiceEndpoint that you provided.

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