0

I'm reading a book which says something about ASP.NET MVC synchronous request handling:

The controller is being run in a web application server that processes only one request at a time and if that all of the requests target the same action method, and the handler starts processing a request only when it has finished processing the previous one, and for the majority of the time, the handler is sitting idle.

So why does ASP.NET MVC not create multiple instances of controllers to serve multiple requests? (I'm not talking about DI which only apply to model classes rather than targeting controller classes) so whenever there is a new request, then the application creates a new instance of target controller, so in this case we don't even need to use async/await?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • What's the book? That isn't anywhere near how I'd describe how ASP.NET. – Flydog57 Oct 16 '19 at 00:38
  • @Flydog57 the book is Expert ASP.NET Web API 2 for MVC Developers by Adam Freeman,Chapter 3 –  Oct 16 '19 at 00:45
  • We can do anything we want, but everything has a cost. Creating new threads is a very costly operation and most of the times will decrease the performance, not increase it. The operations that can benefits from running on new threads are the operations that communicate with external resources and have to wait for those resources to respond or do their tasks. The idea of async/await is that the main thread will be able to do some other stuff while waiting for the external resources. – Racil Hilan Oct 16 '19 at 01:12
  • @RacilHilan thanks for the answer on async/await. But I'm still confused, so why not create new instances of controller class in the main thread to server multiple new requests? –  Oct 16 '19 at 01:22
  • It's been nearly 10 years since I looked at an IIS dump, but this is my understanding of how this works: https://stackoverflow.com/questions/1763775/asp-net-mvc-controller-lifecycle. A request arrives, it gets handed a thread from the thread pool to run on, and a controller gets ginned up and runs the request. That's why I was surprised by the quote you show. – Flydog57 Oct 16 '19 at 04:06
  • 1
    It does create a new instance of the controller for each request. It always does. But a new instance of a class is not the same as a new thread. IIS gives each request a separate thread. The entire request runs on one thread unless it gets migrated from the IO thread to a worker thread under heavy load. I don't know what that excerpt from the book means, we need the context it was in. But the truth is, each request runs on a separate thread and gets a new instance of the controller and any other required class. – Racil Hilan Oct 16 '19 at 13:16

0 Answers0