2011-05-25 28 views
3

我正在使用WCF 4路由服务,并需要以编程方式配置服务(而不是通过配置)。WCF路由 - 如何以编程方式正确添加过滤器表

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

但是,泛型参数到方法应该是TFilterData(数据要筛选上的类型):我已经看到了这样的例子,这是罕见的,如下创建MessageFilterTable?我有我自己的自定义过滤器接受一个字符串 - 我仍然可以这样创建过滤器表?

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

回答

2

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

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

如果你已经写了一个自定义过滤器,那么你将它添加这样的:

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

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

5

我已经创建了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))); 
相关问题