【问题标题】:WCF routing -- how to correctly add filter table programmaticallyWCF 路由——如何以编程方式正确添加过滤表
【发布时间】:2013-05-13 23:25:13
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: wcf wcf-routing


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 1970-01-01
        • 2014-11-10
        相关资源
        最近更新 更多