HttpModule event execution order for multiple modules

I did some tests to find out the event execution order for multiple HttpMoudles in ASP.NET. Here is the what I found:
When there are multiple HttpMoudles, and the modules are registered in Web.config,  all event handlers are executed in the SAME order in which they were registered, except PreSendRequestHeaders and PreSendRequestContent which are in REVERSE order.

Here is an example, suppose we have 3 modules registered in Web.config in following order.

    <modules>
      <add name="Module1" />
      <add name="Module2" />
      <add name="Module3" />
    </modules>

An event, for example BeginRequest, first Module1's BeginRequest will be executed then Module2's and the Module3's. 

This is true for all events except PreSendRequestHeaders  and PreSendRequestContent which are in following order:
So they are in REVERSE order and PreSendRequestContent  and PreSendRequestHeaders in the same module are executed one after another then next module.

In terms of events in one module, this is the execution order:
  • BeginRequest
  • AuthenticateRequest
  • PostAuthenticateRequest
  • AuthorizeRequest
  • PostAuthorizeRequest
  • ResolveRequestCache
  • PostResolveRequestCache
  • MapRequestHandler
  • PostMapRequestHandler
  • AcquireRequestState
  • PostAcquireRequestState
  • PreRequestHandlerExecute
  • PostRequestHandlerExecute
  • ReleaseRequestState
  • PostReleaseRequestState
  • UpdateRequestCache
  • PostUpdateRequestCache
  • LogRequest
  • PostLogRequest
  • EndRequest
  • PreSendRequestContent
  • PreSendRequestHeaders
  • RequestCompleted
This result pretty much confirms what this MSDN document states.

Here is full result from my tests:

BeginRequest
o   Moudle1
o   Moudle2
o   Moudle3
AuthenticateRequest
o   Moudle1
o   Moudle2
o   Moudle3
PostAuthenticateRequest
o   Moudle1
o   Moudle2
o   Moudle3
AuthorizeRequest
o   Moudle1
o   Moudle2
o   Moudle3
PostAuthorizeRequest
o   Moudle1
o   Moudle2
o   Moudle3
ResolveRequestCache
o   Moudle1
o   Moudle2
o   Moudle3
PostResolveRequestCache
o   Moudle1
o   Moudle2
o   Moudle3
MapRequestHandler
o   Moudle1
o   Moudle2
o   Moudle3
PostMapRequestHandler
o   Moudle1
o   Moudle2
o   Moudle3
AcquireRequestState
o   Moudle1
o   Moudle2
o   Moudle3
PostAcquireRequestState
o   Moudle1
o   Moudle2
o   Moudle3
PreRequestHandlerExecute
o   Moudle1
o   Moudle2
o   Moudle3
PostRequestHandlerExecute
o   Moudle1
o   Moudle2
o   Moudle3
ReleaseRequestState
o   Moudle1
o   Moudle2
o   Moudle3
PostReleaseRequestState
o   Moudle1
o   Moudle2
o   Moudle3
UpdateRequestCache
o   Moudle1
o   Moudle2
o   Moudle3
PostUpdateRequestCache
o   Moudle1
o   Moudle2
o   Moudle3
LogRequest
o   Moudle1
o   Moudle2
o   Moudle3
PostLogRequest
o   Moudle1
o   Moudle2
o   Moudle3
EndRequest
o   Moudle1
o   Moudle2
o   Moudle3
PreSendRequestContent
o   Moudle3
PreSendRequestHeaders
o   Moudle3
PreSendRequestContent
o   Moudle2
PreSendRequestHeaders
o   Moudle2
PreSendRequestContent
o   Moudle1
PreSendRequestHeaders
o   Moudle1
RequestCompleted
o   Moudle1
o   Moudle2
o   Moudle3

Final thought: It is not a good idea to have multiple http modules which rely on modules's execution order. Even though the order does seem to be predictable, it is error prone and hard to to debug. It may also have some performance cost. If there is modules which is dependent on another, it'd better just combine them into one.

Print | posted on Monday, October 17, 2016 11:06 AM

Feedback

# re: HttpModule event execution order for multiple modules

Left by jcarry at 10/20/2016 5:26 PM
Gravatar Nice information.keep it up
dr humayun Iqbal

# re: HttpModule event execution order for multiple modules

Left by benchfolks at 9/19/2017 12:40 AM
Gravatar Thanks for sharing an HTTP based code which is very useful for me and for an Android developer.For more details regarding HTTP, you can visit BENCHFOLKS.

# re: HttpModule event execution order for multiple modules

Left by Active Directory at 12/8/2017 8:34 AM
Gravatar Nice information shared. Amazing. Read More

# re: HttpModule event execution order for multiple modules

Left by rebeccahickshq at 2/28/2018 3:30 AM
Gravatar Thank you so much for sharing such useful and interesting information. Your website is highly effective and contains worthy content. I find it more enjoyable than any other show.
bloons tower defense 5
super smash flash 2

# re: HttpModule event execution order for multiple modules

Left by newintrends at 4/15/2018 6:15 PM
Gravatar I just want to say thanks for your wonderful post, it contains a lot of knowledge and information that I needed right now.
newintrends
newintrends

# re: HttpModule event execution order for multiple modules

Left by velevobowi at 5/18/2018 10:39 PM
Gravatar For the growth of the company, it is important to get reviews from the www.mykfcexperience.com customers as their reviews are much needed. The organization can act accordingly to the reviews of the customers.

Your comment:





 

Copyright © Changhong Fu

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski