Multithreading in MVC 4

I am new to ASP.NET MVC (using 4) and have some basic questions regarding multithreading.

  • I have now written all the controllers. Should I explicitly create a thread poll and assign a thread to every incoming request? I read something suggesting that this multithreading is done automatically in MVC and I shouldn't be doing my own. It's true?

  • Most of the queries will modify the database (i.e. download the file). This post says is DbContext

    not thread safe and the selected answer is to create a new instance for every thread I made in my controller. Does this make it safe if MVC automatically creates threads (question # 1)?

Thank!

+3


source to share


1 answer


1) You have nothing to worry about.

On a web server, the .NET Framework maintains a pool of threads that are used to serve ASP.NET requests. When a request arrives, a pooled thread is sent to process that request. If a request is being processed synchronously, the thread that processes the request is blocked while the request is being processed and that thread cannot serve another request.



From here: http://msdn.microsoft.com/en-us/library/ee728598(v=vs.100).aspx

2) You should be fine if you create DbContext

for every request - this is done in your controller constructor, do it for you. (You can also look into the control / dependency structure inversion structure if you want, but that doesn't change the principle.)

+4


source







All Articles