WCF Throttling中的ProcessorCount默认值:Environment.ProcessorCount或处理器数量?

问题描述 投票:1回答:1

我有用.NET 4.0开发的WCF服务在行为中我有这个限制元素:

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

据我所知:WCF 4: Default Throttling Settings for WCF Services

MaxConcurrentSessions:默认值为100 * ProcessorCount MaxConcurrentCalls:默认值为16 * ProcessorCount

在我的电脑上,我有Environment.ProcessorCount = 2

然后我添加了读取这两个值的配置的代码,因为我没有显式添加值,我希望它们是默认值。

我如何从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));
            }
        }
    }
}

结果是:

MaxConcurrentCalls 16 MaxConcurrentSessions 100

现在我有点困惑,什么是默认值?可能是这个检查不正确?提前致谢。

c# wcf throttling
1个回答
2
投票

根据.NET 4.7.1的源代码,您看到的值是正确的。这是因为您正在从配置中读取配置属性,而不是在ServiceThrottle实例中设置的属性。

如果查看ServiceThrottlingElement的代码,您将看到以下属性:

[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; }
}

请注意,MaxConcurrentCalls的默认值设置为ServiceThrottle.DefaultMaxConcurrentCallsMaxConcurrentSession设置为ServiceThrottle.DefaultMaxConcurrentSessions

这些值在ServiceThrottle中定义为:

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

由于配置文件中没有设置值,因此您将收到默认值16和100。

但是,如果你看一下ServiceThrottle的构造函数,你会看到:

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;
}

DefaultMaxConcurrentCallsCpuCountDefaultMaxConcurrentSessionsCpuCount定义如下:

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

因此,当创建新实例时,默认值确实是100 * Processor Count和16 * Processor Count。

© www.soinside.com 2019 - 2024. All rights reserved.