问题描述
为了优化某些服务器端数据库调用,我决定使用System.Threading.Tasks.Task并行化多个数据库调用,然后使用Task.WaitAll()获得所有结果,将其打包并将其发送到客户端通过WCF.在Visual Studio(cassini)中的开发Web服务器上进行测试时,这似乎工作正常,但在部署到IIS时却无法工作.对客户端调用(使用firebug进行性能分析)表明,调用可以到达IIS,但是没有相应的调用被提交到SQL Server.
In order to optimize some server-side database calls I decided to use System.Threading.Tasks.Task to parallelize several database calls then use Task.WaitAll() to get all the results, package them up and send them to the client via WCF. This seems to work fine when testing on the dev web server in Visual Studio (cassini) but does not work when deployed to IIS. Profiling the client calls (with firebug) shows that calls get to IIS but no corresponding calls are submitted to SQL Server.
有人经历过吗?在IIS中使用任务"是否有限制?
Anyone experienced this? Is there a limitation in using Tasks within IIS?
推荐答案
没有直接限制-但是,当您使用Task时,它将在ThreadPool上安排Task.默认情况下,IIS为整个IIS进程共享一个线程池,这可能(尤其是在繁忙的服务器上)导致线程饥饿.这意味着在处理任务时,将使用关于使用ThreadPool的相同指导.有关详细信息,请参见这篇文章.
There is no direct limitation - however, when you use a Task, it schedules the Task on the ThreadPool. IIS, by default, shares a single thread pool for the entire IIS process, which can (especially on a busy server) cause thread starvation to occur. This means that the same guidance about using the ThreadPool applies when working with tasks. See this post for details.
为了查看是否存在此问题,至少可以将其作为测试,使用 TaskCreationOptions.LongRunning 提示.这将导致默认的TaskScheduler在自己的专用(新)线程上创建任务,而不是使用ThreadPool线程.尽管我认为这不是一个长期解决方案的好主意,但您可以验证它是导致线程池不足的原因.如果是这样,您可以确定其他选项,例如可能使用自定义TaskScheduler来管理此操作的线程/任务.
In order to see if this is the problem, you could, at least as a test, generate all of your Task instances with the TaskCreationOptions.LongRunning hint. This will cause the default TaskScheduler to create task on it's own, dedicated (new) Thread instead of using a ThreadPool thread. While I don't think this is a good idea for a long term solution, you would be able to verify that it's thread pool starvation causing your problem. If it is, you could determine other options, such as potentially using a custom TaskScheduler to manage the threads/tasks for this operation.
这篇关于从WCF使用TPL任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!