Search code examples
.netwcfmsmq

How can I get WCF MSMQ to get more then 10 messages at a time?


I've programmatically set the msmq binding and throttling, but even though I specify MaxConcurrentCalls, MaxConcurrentSessions and MaxConcurrentInstances higher then 10 I don't manage to process more then 10 messages at a time.

Here is the code to create the host:

        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();

Here is the service behaviour:

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

I've tried different values for the capacity, all less then 10 works fine, in that I get at most configured, but above 10 will only be 10.


Solution

  • The 10 connections you're allowed are caused by the default on the ServicePointManager.DefaultConnectionLimit.

    In the notes section on the MSDN documentation on that setting it says:

    When used in the server environment (ASP.NET) DefaultConnectionLimit defaults to higher number of connections, which is 10.

    In a none server environment the default is 2.

    To allow for more connections set at application start, when the appdomain loads, the DefaultConnectionLimit.

    ServicePointManager.DefaultConnectionLimit = 100; 
    

    Above line should allow for 100 connections.