1

I am using ASP.NET MVC 3 with .NET 4. I have installed .NET 4.5.1.

When I compare the Thread Id within my HttpModule and Controller.ExecuteCore then I can see that they are different. Why is this the case? In which .NET Framework version was this behavior introduced?

EDIT:

I have observed that when I make a GET request the ManagedThreadId in my HttpModule is the same like in my MVC Controller. But when I make a POST request both are different.

Rookian
  • 19,841
  • 28
  • 110
  • 180
  • Async is a pretty likely reason. Even if you don't use it, the framework might internally. It just pulls a thread from the Thread pool, whichever one is available to work. If your request asynchronously awaits data from the database (as an example, could be file access), that thread will go back into the thread pool until the data comes back. When it does come back, there is no guarantee that the original thread isn't off doing something else. – vcsjones Jan 16 '14 at 22:54
  • In which .NET Framework version was this behavior introduced? – Rookian Jan 17 '14 at 09:37
  • The allowance for it was introduced in .NET Framework 1.0 if IAsyncHttpHandler was used, or BeginXXX and EndXXX. Practically, MVC3 probably introduced it as far as MVC goes with the AsyncController. – vcsjones Jan 17 '14 at 13:59
  • As far as I know MVC3 does not provide any async methods. I don't use any async methods in my project – Rookian Jan 17 '14 at 16:02
  • 3
    That doesn't mean MVC doesn't use async internally, regardless, you can never depend on a single thread handling all of a web request. – vcsjones Jan 17 '14 at 16:06
  • and that's why you can't rely on having single thread process a request - http://stackoverflow.com/questions/8695542/asp-net-mvc3-request-thread-affinity – ivan May 05 '14 at 15:13
  • @Rookian no async methods in MVC 3 (.NET 4.0) does not mean that MVC applicaction does not use threading and Thread Pool. BTW: MVC 4 and .NET 4.5 just simplify asynchronous programming with async and await keywords. Moreover it has nothing to do with threading - do not mix those topics. Calling await in async methods does not mean that awaitable will be resolved in other thread. It just means that the thread calling awaitiable will not be blocked during the wait for response. – Sebastian Xawery Wiśniowiecki Jan 01 '17 at 10:41

1 Answers1

0

ISS uses Thread pool to process every request in the queue. It chooses another thread from thread pool for every request. Asynchronous possibilities of the C# language just give you ability to unblock one of those threads when you predict it will be waiting long time for some calculations or data - mostly data from external services.