2016-11-17 63 views
1

我已經以編程方式設置了msmq綁定和限制,但即使我指定MaxConcurrentCalls,MaxConcurrentSessions和MaxConcurrentInstances高於10,我也無法一次處理超過10條消息。如何讓WCF MSMQ每次獲得10條以上的消息?

下面是代碼來創建主機:

 var host = new ServiceHost(typeof(MqProcessRequestServer)); 
     var binding = new NetMsmqBinding(NetMsmqSecurityMode.None) 
     { 
      UseSourceJournal = true, 
      ReceiveErrorHandling = ReceiveErrorHandling.Drop, 
      ReceiveRetryCount = 0, 
      MaxRetryCycles = 0, 
      RetryCycleDelay = TimeSpan.FromMinutes(1), 
      ExactlyOnce = true, 
      Durable = true, 
      MaxReceivedMessageSize = 4000000000, 
      ReceiveTimeout = TimeSpan.FromSeconds(30) 
     }; 

     var queueUri = string.Format("net.msmq://localhost/private/{0}", scriptEngineVersion); 
     host.AddServiceEndpoint(typeof(IMqProcessRequest), binding, queueUri); 

     // Set throttling to ScriptEngine.Capacity in datamodel 
     var throttling = new ServiceThrottlingBehavior 
     { 
      MaxConcurrentCalls = capacity, 
      MaxConcurrentSessions = capacity, 
      MaxConcurrentInstances = capacity 
     }; 
     host.Description.Behaviors.Add(throttling); 

     // Set service timeout 
     var behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>(); 
     behavior.TransactionTimeout = TimeSpan.FromSeconds(60).ToString(); 

這裏是服務行爲:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall, ReleaseServiceInstanceOnTransactionComplete = false)] 
public class MqProcessRequestServer : IMqProcessRequest, IDisposable 

我已經嘗試了不同的能力值,所有小於10個工作正常,因爲我得到最多配置,但10以上將只有10個。

回答

2

您允許的10個連接是由默認的ServicePointManager.DefaultConnectionLimit引起的。

在關於MSDN documentation上,設置它備註部分表示:

當在服務器環境(ASP.NET)DefaultConnectionLimit默認用於更高數量的連接,這是10

在無服務器環境中,默認值爲2.

爲了允許在應用程序啓動時設置更多連接,當應用程序加載時,DefaultConnectionLimit。

ServicePointManager.DefaultConnectionLimit = 100; 

上面的行應該允許100個連接。