Home > database >  Is an instance of HttpModule dedicated to a single IIS Request from beginning to end?
Is an instance of HttpModule dedicated to a single IIS Request from beginning to end?

Time:12-13

I'm developing an HttpModule to capture logging and performance information for all requests to our web application. I'm tapping into the PreRequestHandlerExecute and PostRequestHandlerExecute events.

When my code executes within PostRequestHandlerExecute, is that code guaranteed to be executing within the same request context as the code which ran earlier on in PreRequestHandlerExecute? In other words, can I preserve state when handling the first event to be used during the execution of the second event?

Or is it possible for an HttpModule instance to service multiple concurrent requests, in which case any state necessary to connect the processing of these two events would need to be stored (and then fetched from) within the current HttpContext?

(I understand of course, that the HttpModule will be used to service multiple requests sequentially and that request-specific state from an earlier request will be irrelevant to a subsequent request. My question is specifically whether a single HttpModule can be used to handle multiple concurrent requests or whether I can rely on the fact that the HttpModule will be dedicated to a single request for the entire lifecycle of that request.)

Thanks for your advice!

CodePudding user response:

As per the literature, and borne out by my own implementation, an HttpModule instance is indeed dedicated to a single Http transaction for the complete Request/Response transaction lifecycle.

https://learn.microsoft.com/en-us/dotnet/api/system.web.httpapplication?source=recommendations&view=netframework-4.8#remarks

https://learn.microsoft.com/en-us/previous-versions/bb470252(v=vs.140)?redirectedfrom=MSDN

State can be safely accumulated in one event to be used in subsequent events because all events the HttpModule instance will receive will belong to the same Request/Response transaction.

(Naturally, any accumulated state which is transaction-specific will need to be reinitialized for the subsequent transaction since an instance of an HttpModule is subject to be reused for a subsequent transaction once the current transaction has completed.)

I have furthermore seen that an instance of an HttpModule may be executing on a different thread than the thread on which it was originally created or previously executed.

  • Related