WCF 节流默认值中的 ProcessorCount:Environment.ProcessorCount 或处理器计数?

ProcessorCount in WCF Throttling defaults: Environment.ProcessorCount or Count of Processors?

我有使用 .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

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

基于 .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。