2011-05-25 30 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))); 
相關問題