Choose HttpHandler or HttpModule? What makes the difference between HttpHandler and HttpModule is that the request can be accessed in these two classes of objects, and the response object will be able to handle requests. I thought I had done so many demos in my blog using ASP. NET to write my own service framework, but some people looked at my examples, still don't know how to choose them, and in order to achieve the same goal, I used HttpHandler, Also useful for HttpModule. It now seems that the examples I designed at that time were not a clear distinction between HttpHandler and HttpModule, but rather a demonstration of how to design a service framework using HttpHandler and HttpModule. Fortunately that blog content did not overreaching, today had to write an article. This article is agreed: 1. HttpHandler refers to all types that implement IHttpHandler interfaces. 2. HttpModule refers to all types that implement IHttpModule interfaces. Therefore, these types and interfaces are not specifically distinguished from this article. Understanding the ASP. HttpHandler and HttpModule are all related to the ASP. So I want to understand that these two types of objects must understand how the ASP. NET pipeline works. Reflects the process flow of the ASP: ASP. This is a time series diagram, we should understand it from two angles: 1. What call actions are available. 2. What are the participants? Each invocation action reflects the processing phase of the ASP, which raises the corresponding events (in addition to Gethandler,processrequest), and HttpModule subscribes to the events and participates in the pipeline process. Some of these phases also raise two events, and a complete pipeline event can be referenced in the MSDN documentation: The picture also reflects the three main contributors to asp: 1. HttpModule 2. Httphandlerfactory 3. Did HttpHandler ever think about how many of these three participants were involved in each of these types of subjects? To answer this question clearly, I prepared the following table: Number of participants per request for pipeline participants HttpModule >= 0 httphandlerfactory 1 HttpHandler 1 why introduce HttphandlerfactoWhere's ry? Please see my blog detail httphandler mapping process, today do not repeat this piece of content. Except for the httphandlerfactory, we can see that in the ASP. HttpHandler, there should be only one, and HttpModule is optional. In turn, we can not understand that: HttpHandler is the protagonist of the processing request (indispensable), HttpModule is a supporting role (can not)? Understand HttpApplication front We've been talking about ASP., so who's in control of the pipeline process? The answer is: HttpApplication object. 1. HttpApplication subdivides its processing and raises different events at different stages, allowing HttpModule to subscribe to events to be added to the requested process. 2. During the processing of the request, the HttpApplication object plays a key role in the process of control and processing. 3. HttpApplication acquires a IHttpHandler instance at a fixed stage and then passes the requested response process to a specific ihttphandler. How is HttpApplication produced and how does it work? 1. The HttpApplication object is reused and is created when httpruntime cannot obtain an idle instance from HttpApplicationFactory. 2. HttpRuntime will hand each request to a HttpApplication object for processing. 3. The HttpApplication object is responsible for loading all the HttpModule at initialization time. 4. Each HttpApplication object controls the pipeline process that belongs to it (explained earlier). HttpApplication is a very important type, and many of its functions are part of the framework's foundation, and we don't need to call it, so we don't use it. I do not want to let the blog overreaching, below to see the protagonist today. Understand HttpHandler before said HttpRuntime will send the request to HttpApplication to handle, at this time you have not thought of such a question: why HttpApplication not directly processing the request, But to give it to a HttpHandler object to handle it? The answer is that the content of each request may be different and there is a multiplicity of them, so that ASP. NET uses the abstract factory pattern to process these requests. Asp. NET in the framework of Web. configAllows us to specify that certain requests are mapped to a httphandlerfactory, such as:<!--configuration for IIS6--><system.web>
Httphandler,httpapplication, HttpModule