2013-06-28 69 views
1

我的WCF服務器是這樣工作的:你可以通過調用服務方法訂閱它們,比如Subscribe()。他們會將您的結果發回給回調通道,說MessageReceived(字符串消息)。WCF RoutingService如何從具有相同合同的兩個不同WCF服務器向客戶端回送消息?

我現在的問題是,我只從一個服務端點獲取回調消息,而不是兩者。事實上,僅僅通過調試,我發現我的第二個服務甚至沒有收到請求。有誰知道問題是什麼?這裏是我的代碼(注意,我在serviceAddresses線二的net.tcp地址):

private void StartAggregatorHost(List<string> serviceAddresses) 
{ 
     // Create a new service host for the routing service (note that RoutingService is a pre-defined Microsoft service model type which routes SOAP messages). 
     aggregatorHost = new ServiceHost(typeof(RoutingService)); 

     // Set up the router address. A logger client will now connect to this address to get logged messages. 
     string fqdn = System.Net.Dns.GetHostEntry("localhost").HostName; 
     string routerAddress = string.Format("net.tcp://{0}:2099/LogAggregator", fqdn); 

     // Set up our router binding. 
     NetTcpBinding routerBinding = new NetTcpBinding(SecurityMode.None, true); 
     routerBinding.SendTimeout = new TimeSpan(0, 1, 0);  
     routerBinding.ReceiveTimeout = new TimeSpan(25, 0, 0); 
     routerBinding.MaxReceivedMessageSize = int.MaxValue; 
     routerBinding.MaxConnections = int.MaxValue; 
     routerBinding.ListenBacklog = int.MaxValue; 
     routerBinding.ReliableSession.Enabled = true; 
     routerBinding.ReliableSession.Ordered = true; 
     routerBinding.ReliableSession.InactivityTimeout = new TimeSpan(15, 0, 0, 0); 

     // Define the type of router in use. For duplex sessions like in our case, we want to use the IDuplexSessionRouter. 
     Type contractType = typeof(IDuplexSessionRouter); 

     // Add the endpoint that the router will use to recieve and relay messages. Note the use of System.ServiceModel.Routing.IDuplexSessionRouter. 
     aggregatorHost.AddServiceEndpoint(contractType, routerBinding, routerAddress); 

     // Create the endpoint list that contains the service endpoints we want to route to. 
     List<ServiceEndpoint> endpointList = new List<ServiceEndpoint>(); 

     foreach (string serverAddress in serviceAddresses) 
     { 
      // Set up our server binding(s) for each server. 
      NetTcpBinding serverBinding = new NetTcpBinding(SecurityMode.None, true); 
      serverBinding.SendTimeout = new TimeSpan(0, 1, 0); 
      serverBinding.ReceiveTimeout = new TimeSpan(25, 0, 0); 
      serverBinding.MaxReceivedMessageSize = int.MaxValue; 
      serverBinding.MaxConnections = 1; 
      serverBinding.ListenBacklog = int.MaxValue; 
      serverBinding.ReliableSession.Enabled = true; 
      serverBinding.ReliableSession.Ordered = true; 
      serverBinding.ReliableSession.InactivityTimeout = new TimeSpan(15, 0, 0, 0); 

      // Create the server endpoint the router will route messages to and from. 
      ContractDescription contract = ContractDescription.GetContract(contractType); 
      ServiceEndpoint server = new ServiceEndpoint(contract, serverBinding, new EndpointAddress(serverAddress)); 

      // Add the server to the list of endpoints. 
      endpointList.Add(server); 
     } 

     // Create a new routing configuration object. 
     RoutingConfiguration routingConfiguration = new RoutingConfiguration(); 

     // Add a MatchAll filter to the Router's filter table. Map it to the endpoint list defined earlier. When a message matches this filter, it will be sent to the endpoint contained in the list. 
     routingConfiguration.FilterTable.Add(new MatchAllMessageFilter(), endpointList); 

     // Attach the behavior to the service host. 
     aggregatorHost.Description.Behaviors.Add(new RoutingBehavior(routingConfiguration)); 

     // Open the service host. 
     aggregatorHost.Open(); 



     m_eventLog.WriteEntry(string.Format("Log aggregator service hosted at {0}.", routerAddress), EventLogEntryType.Information); 

} 

所以再次...這就是我想要的:

CLIENT ---REQ---> ROUTER ---REQ---> SVC1 
         ---REQ---> SVC2 

CLIENT <---CALLBACK1--- ROUTER <---CALLBACK1--- SVC1 
     <---CALLBACK2---  <---CALLBACK2--- SVC2 

這是我「M獲得(即使我添加第二個服務,我的路由器似乎它甚至不調用它的服務方法):

CLIENT ---REQ---> ROUTER ---REQ---> SVC1 

CLIENT <---CALLBACK--- ROUTER <---CALLBACK--- SVC1 
+1

您可以檢查的幾件事: - 當您嘗試手動調用它們時,SVC1和SVC2是否正確運行? - 嘗試逐個添加服務端點而不是添加列表 - 嘗試指定添加到FilterTable時的優先級(SVC1和SVC2的優先級相同) - 嘗試首先儘快使用XML路由配置文件因爲它的工作切換到代碼 – MaxSC

+0

@ MaxS-Betclic - 發佈此作爲答案現在,你的英雄。這是我錯過的優先事項。我猜想傳入列表會自動生成優先級,因此您需要逐個添加每個服務並指定其優先級:routingConfiguration.FilterTable.Add(new MatchAllMessageFilter(),new List {yourService},1); – Alexandru

+0

不客氣! – MaxSC

回答

1

您應該設置一個特定的優先級加法SVC1 & SVC2到FilterTable。

routingConfiguration.FilterTable.Add(new MatchAllMessageFilter(), new List<YourServiceType> { yourService }, 1); 

更多信息here

相關問題