WCF 服务是否使用多个线程来处理传入请求?
如何确保 WCF 服务使用 ThreadPool 中的线程来处理传入消息?
目前简单的方法调用,如“return null;”处理另一个请求大约需要 45 秒
这是我注释服务类的方式:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public partial class MyService : IMyService {
...
}
但是当我在任务管理器中观察进程时,它似乎正在使用恒定数量的线程。即使在负载下。
public ActionResult SelectDatabase(string param)
{
if (!String.IsNullOrEmpty(param))
{
try
{
MyServicece svc = new MyService();
Database[] dbsArray = svc.GetDatabases(param);
if (depsArray != null)
ViewData["depsArray"] = depsArray;
return View();
}
catch (Exception exc)
{
// log here
return ActionUnavailable();
}
}
这是我的服务行为:
<?xml version="1.0"?>
<configuration>
<runtime>
</runtime>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
<system.serviceModel>
<diagnostics performanceCounters="Default" />
<bindings>
<netTcpBinding>
<binding sendTimeout="00:02:00" receiveTimeout="00:02:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
<security mode="None">
</security>
</binding>
</netTcpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<endpointBehaviors>
<behavior name="CrossDomainServiceBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyService.MyServiceBehavior">
<serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="100" maxConcurrentSessions="100" />
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyService.MyServiceBehavior" name="MyService.MyService">
<endpoint address="MyService" binding="netTcpBinding" contract="AService.IAServ" isSystemEndpoint="false" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="MyService.MyServiceBehavior" name="MyService.MyServiceAdmin">
<endpoint address="MyServiceAdmin" binding="netTcpBinding" contract="MyService.IMyServiceAdmin" isSystemEndpoint="false" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
这是我创建服务实例的方式:
ServiceHost myserviceHost = new ServiceHost(typeof(MyService), new Uri(String.Format("net.tcp://{0}/", _bindAddress)));
myserviceHost.Open();
Console.WriteLine(myserviceHost.BaseAddresses[0]);
How can I ensure that a WCF service uses threads from a ThreadPool to process incoming messages?
At the moment simple method invocation like 'return null;' takes about 45 seconds while another requests are processing
Here is how I have annotated my service class:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public partial class MyService : IMyService {
...
}
But when I'm watching the process in task manager it seems to be using a constant number of threads. Even under load.
public ActionResult SelectDatabase(string param)
{
if (!String.IsNullOrEmpty(param))
{
try
{
MyServicece svc = new MyService();
Database[] dbsArray = svc.GetDatabases(param);
if (depsArray != null)
ViewData["depsArray"] = depsArray;
return View();
}
catch (Exception exc)
{
// log here
return ActionUnavailable();
}
}
Here is my service behavior:
<?xml version="1.0"?>
<configuration>
<runtime>
</runtime>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
<system.serviceModel>
<diagnostics performanceCounters="Default" />
<bindings>
<netTcpBinding>
<binding sendTimeout="00:02:00" receiveTimeout="00:02:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
<security mode="None">
</security>
</binding>
</netTcpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<endpointBehaviors>
<behavior name="CrossDomainServiceBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyService.MyServiceBehavior">
<serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="100" maxConcurrentSessions="100" />
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyService.MyServiceBehavior" name="MyService.MyService">
<endpoint address="MyService" binding="netTcpBinding" contract="AService.IAServ" isSystemEndpoint="false" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="MyService.MyServiceBehavior" name="MyService.MyServiceAdmin">
<endpoint address="MyServiceAdmin" binding="netTcpBinding" contract="MyService.IMyServiceAdmin" isSystemEndpoint="false" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
Here is how I create service instance:
ServiceHost myserviceHost = new ServiceHost(typeof(MyService), new Uri(String.Format("net.tcp://{0}/", _bindAddress)));
myserviceHost.Open();
Console.WriteLine(myserviceHost.BaseAddresses[0]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
InstanceContextMode 和 ConcurrencyMode 是独立的概念,但具有一定程度的相互作用 - 我不久前在博客中对此进行了深入讨论
WCF 调用是在 IO 线程池上处理的线程。假设您还没有执行诸如
ConcurrencyMode.Single
、InstanceContextMode.Single
之类的操作,这些操作将序列化对服务的每个调用,线程池管理器将尝试将线程数平衡到工作率。如果并发请求的数量可以由 5 个线程提供服务,那么这就是它将使用的数量。您可能会发现线程池可以跟上您可以看到的线程数量的工作速率。您可以非常高兴地使用比内核更多的线程,因为只要线程不纯粹受 CPU 限制,操作系统就可以在先前运行的线程启动 IO 时通过将线程切换到 CPU 来获得吞吐量。如果 CPU 完全耗尽,那么线程池管理器的启发式方法将使其不愿向线程池中添加更多线程。
但是,还有另外两个潜在问题:
InstanceContextMode and ConcurrencyMode are separate concepts but which have a level of interplay - I blogged about this in some depth a while back
WCF calls are processed on IO threadpool threads. Assuming you haven't done something like
ConcurrencyMode.Single
,InstanceContextMode.Single
which will serialize every call into the service the threadpool manager will try to balance the number of threads to the rate of work.If the number of concurrent requests can be serviced by 5 threads then that's how many it will use. You may be seeing that the threadpool can keep up with the rate of work with the number of threads you can see. You can quite happily use more threads than cores with effect because, as long as the threads are not purely CPU bound, the OS can gain throughput by switching threads onto the CPU when the previously running thread starts IO. If the CPU is completely max'd out then the heuristics of the threadpool manager will make it reticent to add more threads into the thread pool
However, there are another couple of potential issues: