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

问题描述

| 我正在使用WCF 4路由服务,并且需要以编程方式配置服务(而不是通过config)。我所见过的示例很少见,创建了一个MessageFilterTable,如下所示:
            var filterTable=new MessageFilterTable<IEnumerable<ServiceEndpoint>>();
但是,该方法的通用参数应该是TFilterData(正在过滤的数据类型)吗?我有一个接受字符串的自定义过滤器-我仍然可以这种方式创建过滤器表吗? 如果这行得通...路由基础架构会在我传入的列表之外创建客户端端点吗?     

解决方法

我创建了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\",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)));
    ,您可以在此处在MSDN上找到一个很好的示例:如何:动态更新路由表 请注意,它们如何不直接创建MessageFilterTable的实例,而是使用新的RoutingConfiguration实例提供的\'FilterTable \'属性。 如果您编写了一个自定义过滤器,则可以这样添加它:
rc.FilterTable.Add(new CustomMessageFilter(\"customStringParameter\"),new List<ServiceEndpoint> { physicalServiceEndpoint });
CustomMessageFilter将是您的过滤器,\“ customStringParameter \”是您正在谈论的字符串(我相信)。 当路由器收到连接请求时,它将尝试通过此表条目进行映射,如果成功,则您是对的,路由器将创建一个客户端终结点以与您提供的ServiceEndpoint进行通信。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...