views:

130

answers:

3

Looking at a very simple web project, hosted in IIS, with one simple aspx page, executing some code (get some data from a db and populate some controls), which of the following is true:

each page request shares the same instance of the codebehind class.
or each page request runs in its own instance of the codebehind class.

Does one thread/instance run against each aspx page. Or does one thread/instance cover multiple pages?

Im trying to understand, in a simple web project, that receives 100 page requests, will they run consecutively one after the other, or multiple instances/threads for each request?

+3  A: 
  • Each request gets a new instance of the code-behind class.
  • One instance of the code-behind class serves one request.
  • Two requests at different points in time may run on the same thread from the thread pool.
  • Two requests that runs in parallell gets one thread each (I think; not 100% sure on if there are some corner cases that I am not aware of regarding the threading).

So, a web server can handle several requests in parallell, but there is of course a limit on how many requests that can be served at the same time.

Fredrik Mörk
+1  A: 

Maybe take a look at asynchronous ASP.NET?

http://msdn.microsoft.com/en-us/magazine/cc163725.aspx

Normally a new thread is assigned to a new request.

"A normal, or synchronous, page holds onto the thread for the duration of the request, preventing the thread from being used to process other requests."

IrishChieftain
+2  A: 

There is one instance of the code behind class for each request.

Well, actually it's an instance of the aspx page class, that inherits from the code behind class. (That's why you are using the protected keyword for event handlers in code behind, so that the inheriting class can access them.)

There is also the case where you do a Server.Transfer or Server.Execute, then the request is transfered to another page instance.

There are several threads running in the IIS handling requests, and usually one request is handled in one thread, but a request can be moved from one thread to another in some situations.

If 100 requests arrive at the server, it will start processing a few of them in separate threads, and put the rest of them in a queue. Noteworthy is that the server will only process one page at a time from each user, so unless you use sessionless pages (to make them anonymous) it will not process two pages for the same user in parallel threads, which makes the whole threading part a lot easier.

Guffa