1
votes

I have WCF service developed with .NET 4.0 In behavior I have this throttling element:

<behavior name="Test">
  <serviceThrottling maxConcurrentInstances="1000"/>
</behavior>

As I know according to this: WCF 4: Default Throttling Settings for WCF Services

MaxConcurrentSessions: default is 100 * ProcessorCount MaxConcurrentCalls: default is 16 * ProcessorCount

On my PC I have Environment.ProcessorCount = 2

I then added code that reads config of these two values, since I didn't add values explicitly I expect them to be default.

How I check it from Global.asax:

protected void Application_Start(object sender, EventArgs e)
{
    var config = WebConfigurationManager.OpenWebConfiguration("/TestDefaults");

    var bindings = BindingsSection.GetSection(config);
    var group = ServiceModelSectionGroup.GetSectionGroup(config);

    foreach (ServiceBehaviorElement behavior in group.Behaviors.ServiceBehaviors)
    {
        if (behavior.Name == "Test")
        {
            var th = ((ServiceThrottlingElement)behavior.Where(el => el is ServiceThrottlingElement).FirstOrDefault());

            if (th != null)
            {
                File.AppendAllText(Server.MapPath("~/Result.txt"), 
                    String.Format("MaxConcurrentCalls {0} MaxConcurrentSessions {1}",
                    th.MaxConcurrentCalls,
                    th.MaxConcurrentSessions));
            }
        }
    }
}

Result is:

MaxConcurrentCalls 16 MaxConcurrentSessions 100

Now I'm a bit confused, what are defaults? Could be that this check is not correct? Thanks in advance.

1

1 Answers

3
votes

Based on the source code for .NET 4.7.1, the values you saw are correct. This is because you're reading configuration properties from the config, not the properties set in the instance of ServiceThrottle.

If you look at the code for ServiceThrottlingElement, you will see the following properties:

[ConfigurationProperty(ConfigurationStrings.MaxConcurrentCalls, DefaultValue = ServiceThrottle.DefaultMaxConcurrentCalls)]
[IntegerValidator(MinValue = 1)]
public int MaxConcurrentCalls
{
    get { return (int)base[ConfigurationStrings.MaxConcurrentCalls]; }
    set { base[ConfigurationStrings.MaxConcurrentCalls] = value; }
}

[ConfigurationProperty(ConfigurationStrings.MaxConcurrentSessions, DefaultValue = ServiceThrottle.DefaultMaxConcurrentSessions)]
[IntegerValidator(MinValue = 1)]
public int MaxConcurrentSessions
{
    get { return (int)base[ConfigurationStrings.MaxConcurrentSessions]; }
    set { base[ConfigurationStrings.MaxConcurrentSessions] = value; }
}

Note that the default value for MaxConcurrentCalls is set to ServiceThrottle.DefaultMaxConcurrentCalls and MaxConcurrentSession is set to ServiceThrottle.DefaultMaxConcurrentSessions

These values are defined in ServiceThrottle as:

internal const int DefaultMaxConcurrentCalls = 16;
internal const int DefaultMaxConcurrentSessions = 100;

Since there were no values set in the configuration file, you're receiving the default values of 16 and 100.

If you look at the constructor for ServiceThrottle, however, you'll see this:

internal ServiceThrottle(ServiceHostBase host)
{
    if (!((host != null)))
    {
        Fx.Assert("ServiceThrottle.ServiceThrottle: (host != null)");
        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("host");
    }
    this.host = host;
    this.MaxConcurrentCalls = ServiceThrottle.DefaultMaxConcurrentCallsCpuCount;
    this.MaxConcurrentSessions = ServiceThrottle.DefaultMaxConcurrentSessionsCpuCount;
    this.isActive = true;
}

DefaultMaxConcurrentCallsCpuCount and DefaultMaxConcurrentSessionsCpuCount are defined as follows:

internal static int DefaultMaxConcurrentCallsCpuCount = DefaultMaxConcurrentCalls * OSEnvironmentHelper.ProcessorCount;
internal static int DefaultMaxConcurrentSessionsCpuCount = DefaultMaxConcurrentSessions * OSEnvironmentHelper.ProcessorCount;

So when a new instance is created, the default values are indeed 100 * Processor Count and 16 * Processor Count.