自动调整WCF到当前资源?

时间:2013-06-11 13:27:48

标签: performance wcf settings throttling

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

BestRegards

1 个答案:

答案 0 :(得分:2)

事实上,自.net 4以来已经是这种情况(参见博客文章here)。 但是,只有处理器的数量很重要。

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

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

然而,WCF限制,即WCF限制只是一种行为。您可以在启动时使用ServiceHost自定义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" %>