
>现在,我已经编写了所有控制器.是否应该显式创建线程轮询并为每个传入请求分配线程?我读到一些东西暗示这种多线程是在MVC中自动完成的,我不应该自己做.这是真的?
>大多数请求都会更改数据库(即上传文件). post表示DbContext不是线程安全的,选择的答案是为每个线程创建一个新实例,这是我在控制器中所做的.如果MVC自动创建线程(问题1),这是否安全?
谢谢!
On the Web server, the .NET Framework maintains a pool of threads that are used to service ASP.NET requests. When a request arrives, a thread from the pool is dispatched to process that request. If the request is processed synchronously, the thread that processes the request is blocked while the request is being processed, and that thread cannot service another request.
从这里:http://msdn.microsoft.com/en-us/library/ee728598(v=vs.100).aspx
2)如果要为每个请求构造一个DbContext,则应该没问题-在Controller的Constructor中执行此操作将为您完成此操作. (如果需要,您也可以查看控制反转/依赖注入框架,但这不会改变原理.)
转载注明原文:c#-MVC 4中的多线程 - 乐贴网