1

是否可以使 WCF 服务适应当前资源,例如,如果 ram 较低,则将并发调用更改为较低的 nr?

此致

4

1 回答 1

2

事实上,从 .net 4 开始就是这样(请参阅此处的博文)。但是,只有处理器的数量很重要。

  • MaxConcurrentSessions:默认为 100 * ProcessorCount
  • MaxConcurrentCalls:默认为 16 * ProcessorCount
  • MaxConcurrentInstances:默认是上述两个的总和,遵循与之前相同的模式。

HTTP 是一种无状态协议,建议避免在服务器端使用会话,以实现可扩展性。一个典型的前端服务器,消耗 CPU 而不是内存(ScaleOut Vs ScaleUp)。

但是,WCF 限制,也就是 WCF 限制只是一种行为。您可以在启动时使用自定义 ServiceHostFactory 添加/编辑此行为。

ServiceThrottlingBehavior throttle = new ServiceThrottlingBehavior();
throttle.MaxConcurrentCalls = /*what you want*/;
throttle.MaxConcurrentSessions = /*what you want*/;
throttle.MaxConcurrentInstances = /*what you want*/; 

ServiceHost host = new ServiceHost(typeof(TestService));    
// if host has behaviors, remove them.
if (host.Description.Behaviors.Count != 0)
{
   host.Description.Behaviors.Clear();
}
// add dynamically created throttle behavior
host.Description.Behaviors.Add(throttle);

//open host
host.Open();

您可以使用自定义ServiceHostFactory来实现相同的目的。

<%@ ServiceHost Language="C#" Debug="true" Service="MyWebApplication.TestService"
                Factory="MyWebApplication.TestServiceHostFactory" %>
于 2013-06-11T14:20:20.240 回答