Why the IHttpModule. Init method is executed multiple times

Source: Internet
Author: User

I found that many of my friends, like me, used IHttpModule. Init as a substitute for Application_Start, and initialized some applications.

However, the Application_Start events in IHttpModule. Init and Global. asax are different in nature. Instead of using IHttpModule. Init to replace Application_Start for initialization of ASP. NET applications. You cannot simply use the Init method to repeatedly call it to determine that the ASP. NET program has restarted.

The reason is that IHttpModule. Init may be repeatedly called multiple times in response to ASP. NET requests, which is more likely to happen during actual website operation.

Why IHttpModule. the reason why Init is called multiple times is that each HttpApplication instance can process only one request at the same time, while ASP. NET supports a certain number of concurrent requests. Therefore, when the instance of HttpApplication is insufficient to respond to concurrent requests, multiple requests will be created to respond to different requests, after each HttpApplication instance is created, a new set of httpmodules are created and the Init method is called.

Application_Start will only be called after the first HttpApplication object is created, and the subsequent HttpApplication instance will not trigger this event.

I think the reuse of the HttpApplication instance is one of the main reasons for misunderstanding of the IHttpModule Init method usage, because we usually only have one request when debugging the program, basically, it is impossible to repeatedly execute the Init method of HttpModule. In the actual website operating environment, concurrent requests are common. Misuse of the Init method may cause strange problems in the actual environment.

For details, refer to the document ASP. NET application lifecycle overview in IIS 5.0 and 6.0 on MSDN. The following is an image provided in the article:

You can also use Refelector to decompile the System. Web assembly and analyze the call relationship of the IHttpModule. Init method,

Finally, you will find the System. Web. HttpApplicationFactory. GetNormalApplicationInstance method, which shows how the HttpApplication instance is reused and created.

To sum up, IHttpModule. Init cannot be a substitute for Application_Start.

A simple method is to use a static bool field as the initialization tag. After the Init of HttpModule executes the initialization required by the program, it sets the flag to true, the initialization will not be repeated next time.

However, code like registering a BeginRequest event still needs to be executed every time in Init, because the HttpModule instance is different at this time, if the static initialization tag field is determined and the event is not registered repeatedly, it may lead to a strange issue that similar URL rewriting sometimes fails to be executed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.