WCF服务是否使用多个线程来处理传入请求
本文关键字:处理 请求 线程 是否 服务 WCF | 更新日期: 2023-09-27 18:24:59
如何确保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]);
InstanceContextMode和ConcurrencyMode是独立的概念,但它们有一定程度的相互作用-我在前的博客中对此进行了深入的讨论
WCF调用是在IO线程池线程上处理的。假设您还没有执行类似ConcurrencyMode.Single
、InstanceContextMode.Single
的操作,它将把每个调用序列化到服务中,线程池管理器将尝试平衡线程数量与工作速率。
如果并发请求的数量可以由5个线程提供服务,那么这就是它将使用的数量。您可能会看到线程池可以跟上您所能看到的线程数量的工作速度。你可以很高兴地使用比核心更多的线程,因为只要线程不是纯粹的CPU绑定,当之前运行的线程启动IO时,操作系统可以通过将线程切换到CPU来获得吞吐量。如果CPU完全耗尽,那么线程池管理器的启发式将使其不愿向线程池中添加更多线程
然而,还有另外几个潜在的问题:
- 当通过同一代理存在多个并发出站请求时,基于会话的绑定可能会在客户端阻塞。你没有;不要说你是如何生成多个请求的,所以这可能是一个问题
- 您可能还会看到节流正在发挥作用,因为在.NET 4之前,默认的最大并发请求数为16,默认的并发会话数为10。这些值在.NET 4中已被提升,但您没有说明您正在使用的.NET版本