3

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

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

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

如果这可行...路由基础架构会在我传入的列表之外创建客户端端点吗?

4

2 回答 2

5

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)));
于 2011-11-10T04:58:56.443 回答
3

您可以在 MSDN 上找到一个很好的示例:How To: Dynamic Update Routing Table

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

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

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

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

于 2011-06-26T20:51:55.863 回答