kse*_*een 5 .net c# wcf multithreading threadpool
如何确保WCF服务使用ThreadPool中的线程来处理传入的消息?
目前简单的方法调用,如'return null;' 在处理另一个请求时需要大约45秒
这是我如何注释我的服务类:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public partial class MyService : IMyService {
...
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在任务管理器中观察进程时,它似乎使用了一定数量的线程.即使在负载下.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的服务行为:
<?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>
Run Code Online (Sandbox Code Playgroud)
以下是我创建服务实例的方法:
ServiceHost myserviceHost = new ServiceHost(typeof(MyService), new Uri(String.Format("net.tcp://{0}/", _bindAddress)));
myserviceHost.Open();
Console.WriteLine(myserviceHost.BaseAddresses[0]);
Run Code Online (Sandbox Code Playgroud)
InstanceContextMode和ConcurrencyMode是单独的概念,但它们具有相互作用的水平 - 我在一段时间内对此进行了博客论述
WCF调用在IO线程池线程上处理.假设你没有做过类似的事情ConcurrencyMode.Single,InstanceContextMode.Single它会将每个调用序列化到服务中,线程池管理器将尝试平衡线程数与工作速率.
如果5个线程可以处理并发请求的数量,那么它将使用多少个.您可能会看到线程池可以跟上您可以看到的线程数的工作速度.您可以非常高兴地使用比核心更多的线程,因为只要线程不是纯粹的CPU绑定,操作系统就可以通过在先前运行的线程启动IO时将线程切换到CPU来获得吞吐量.如果CPU完全最大化,那么线程池管理器的启发式将使它更加谨慎地将更多线程添加到线程池中
但是,还有另外两个潜在的问题: