Asp.net HttpModule and HttpApplication usage (1/3)

Source: Internet
Author: User

What is ihttpmodule? What is the use of web development?
First, we can see from the name that it is an interface, which is inherited by people. We have to inherit it and implement its method. Module refers to modules and components. If we have implemented this interface and configured the web. config to let iis know that our web program uses this component. Is our program more than the default web program ?! Obviously, the methods defined in our components will be called when necessary, which is the use of httpmodule. To put it bluntly, we write an extension for iis, but this extension is only for web programs that use (configure config. In fact, every web application is an iis process, and the configuration file of this process is web. config.
After understanding his meaning, we can start! Create a web application, create a class, inherit ihttpmodule, and implement its method. In the modules node of config, <add name = "" type = ""/>, OK!

Namespace webapplication1
{
Public class myhttpmodule: ihttpmodule
    {
Public void dispose ()
        {
        }

Public void init (httpapplication context)
        {
Context. context. response. write (1 );
        }
    }
}


<? Xml version = "1.0"?>
<Configuration>
<System. web>
<Compilation debug = "true" targetframework = "4.0"/>
<Httpmodules>
<Add name = "myhttpmodule" type = "webapplication1.myhttpmodule, webapplication1"/>
</Httpmodules>
</System. web>

<System. webserver>
<Modules runallmanagedmodulesforallrequests = "true">
<Add name = "myhttpmodule" type = "webapplication1.myhttpmodule, webapplication1"/> <! -- Note: type = "class fullname, class dll name" -->
</Modules>
</System. webserver>
</Configuration>

There are two web. config configurations. The above one is for non-iis7, and the following is obviously for iis7. Start the program, what happend ?! Is there an additional 1 in the header of the page !! When we open any page, there will be a 1, indicating that our module has played a role, and that every request will execute the init method of httpmodule? Right?
Let's change the code:

 

Public void init (httpapplication context)
 {
Context. context. response. write (1 );
Context. beginrequest + = onbeginrequest;
 }

Public void onbeginrequest (object sender, eventargs e)
 {
Var app = (httpapplication) sender;
Var url = app. context. request. rawurl;
App. context. response. write (url );
 }

Add a breakpoint to the init and onbeginrequest methods, re-compile them, and then f5 will look at it. Init only takes 1 time, while onbeginrequest does. The values of ur are default. asp tutorial x style.css tutorial and favorite. ico; we can see that any url request, including static files, will be executed by the event method we defined! It seems that this is much slower than processing only aspx!
The init operation must be performed once. Otherwise, the event will not be subscribed three times ?, But why does it take only one time? Why? Haha, it is actually very simple. The myhttpmodule will be instantiated once. After instantiation, execute init initialization and then it will reside in the application pool until the application pool is recycled, or he is crashed for various reasons, while onbeginrequest is subscribed to by the beginrequest event of the httpapplication class. What is event subscription? An event is a special delegate. What is delegation? A delegate is a method pointer. Therefore, as long as the delegate is executed, it will execute the method body to which it points, that is, onbeginrequest. It can be seen that the onbeginrequest execution is related to the beginrequest of httpapplication, it has nothing to do with myhttpmodule itself.
After three requests, it indicates that all three requests have executed beginrequest. Is each request instantiated an httpapplication? I can see from the name that no, because application (application) Well, we currently run one, how can we continuously instantiate it! If you want to solve the root problem and thoroughly understand it, you have to go through the framework source code and debug it!
(------------ Declaration, the following source code can be skipped without full understanding, as long as you know that it is related to the request ------------)
Next we will investigate the initialization process of httpapplication!
Use reflector to check the classes in the system. web namespace. You can see the httpapplicationfactory class, which is responsible for creating the httpapplication. When we started the site, it was slow for the first time. Why? Because of the initialization build work.
System. web. there are a bunch of build classes under the complilation namespace, including building global. asax, that is, our httpapplication class, is then cached into the factory stack and pop out when we need it. (You may have doubts, isn't pop gone? In fact, the app will push back during execution. For details, see httpapplication. releaseappinstance method)

Homepage 1 2 3 Last page
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.