Preliminary understanding and understanding of ASP. NET underlying layer

Source: Internet
Author: User

Recently, some good articles have been found on overseas websites, collected and sorted for your own understanding, recorded as notes, so that you can have memories in the future.

ASP. NET is a very powerful platform for building Web applications. It provides great flexibility and capabilities so that it can be used to build all types of Web applications. most people are only familiar with high-level frameworks such as WebForms and WebServices. the. NET hierarchy is at the highest level. in this article, I will discuss ASP. and explains how requests are transmitted from the Web server to ASP. NET runtime and then how to use ASP.. NET pipeline to process requests.

ASP. NET is a request processing engine. it receives a sent request and sends it to the internal pipeline until the end. As a developer, you can add some code here to process the request. this engine is completely separated from HTTP/Web servers. in fact, HTTP Runtime is a component that allows you to get rid of IIS or any other server programs and host your own programs.

The runtime provides a complex but elegant mechanism for routing requests in pipelines. there are many related objects, most of which are eXtensible (through inheritance or event interfaces), which is true for almost all processing procedures. therefore, this framework is highly scalable. this mechanism makes it possible to connect to very underlying interfaces (such as cache, authentication, and authorization. you can even filter the content after preprocessing or processing, or simply route requests that match the special mark directly to your code or another URL. there are many different methods to accomplish the same thing, but all of these methods can be implemented simply and directly. At the same time, flexibility is provided to achieve the best performance and simplicity of development.

The entire ASP. the NET engine is fully built on managed code, and all extension functions are provided through managed code extension. this is true. NET Framework has the best proof of building a complex and efficient framework. ASP. the most impressive part of NET is its well-thought-out design, which makes the framework very easy to use and provides the ability to handle almost all parts of a request.

ASP. NET interacts with IIS through the ISAPI extension on Microsoft's platform. This extension is hosted. NET runtime and ASP. NET runtime. ISAPI provides the core interface, ASP.. NET uses the unmanaged ISAPI code to obtain requests from the Web server through this interface and send responses back to the client. the content provided by ISAPI can be obtained through common objects (such as HttpRequest and HttpResponse). These objects expose unmanaged data through a well-defined and accessible interface.

When a user sends a URL request, the request is obtained on the Web server side, IIS5 or 6. at the bottom layer, ASP.. NET and IIS interact with each other through the ISAPI extension. in ASP. in the. NET environment, this request is usually routed to a. NET extension. on the aspx page, but how this process works depends entirely on how HTTP Handler with a specific extension is implemented. in IIS. aspx is mapped to ASP through 'application extension' (also known as script ing. net isapi extension DLL-aspnet_isapi.dll. each request needs to trigger ASP through an extension registered to aspnet_isapi.dll. NET (to process this request ).

ISAPI is the underlying unmanaged Win32 API. the interface defined by ISAPI is very simple and optimized for performance. they are very underlying-processing pointers and function pointers for callback-but they provide the lowest-level and efficiency-oriented interfaces, this allows developers and tool providers to use it to connect to IIS. because ISAPI is very low-level, it is not suitable for developing application-level code, and ISAPI tends to be mainly used for bridging interfaces. The upstream tool provides the application server type function.

The following describes HttpRuntime, HttpContext, and HttpApplication.

When a request arrives, it is routed to the ISAPIRuntime. ProcessRequest () method. This method calls the HttpRuntime. ProcessRequest method and performs some important tasks.

Create a new HttpContext instance for the request
Get an HttpApplication instance
Call HttpApplication. Init () to set the events of the MPs queue.
The Init () method triggers the HttpApplication. ResumeProcessing () method for starting ASP. NET pipeline processing.

First, a new HttpContext object is created and used to pass ISAPIWorkerRequest. This context is always available throughout the request lifecycle and can always be passed through static attributes.
HttpContext. currect to access. as the name implies, the HttpContext object represents the context of the current active Request because it contains all the typical important objects you need to access in the Request lifecycle: Request, Response, application, Server, Cache. httpContext. current gives you access to all these capabilities.

The HttpContext object also contains a very useful Items set. You can use it to save data for specific requests. the context object is created at the beginning of the Request cycle and released at the end of the request. All data stored in the Items set is only available in this specific request. A good example is the request log mechanism. the Application_BeginRequest and Application_EndRequest Methods attached to asax record the request start and end times (as shown in figure 3 ). httpContext is very useful to you-if you need data in different parts of the request or page processing, you can use it freely.

Protected void Application_BeginRequest (Object sender, EventArgs e)
{
If (App. Configuration. LogWebRequests)
{
Context. Items. Add ("WebLog_StartTime", DateTime. Now );
}
}

Protected void Application_EndRequest (Object sender, EventArgs e)
{
If (App. Configuration. LogWebRequests)
{
Try
{
TimeSpan Span = DateTime. Now. Subtract (DateTime) Context. Items ["WebLog_StartTime"]);
Int MiliSecs = Span. TotalMilliseconds;
WebRequestLog. Log (App. Configuration. ConnectionString, true, MilliSecs );
}
}
}

HttpApplication

Each request is routed to an HttpApplication object. the HttpApplicationFactory class serves your ASP.. NET application creates an HttpApplication Object pool and distributes references to the HttpApplication object for each request. the size of the Object pool is affected by machine. the MaxWorkerThreads setting limit in the ProcessModel key in the config file.
HttpApplication is the external wrapper of your Web program, and it is mapped to Global. class defined in asax. it is the first entry point to enter HttpRuntime. if you view Global. asax (or the corresponding code class) you will find that this class directly inherits from HttpApplication:
The main responsibility of HttpApplication is to act as the event controller of the Http pipeline. Therefore, its interfaces mainly contain events. Event mounting is very extensive, including the following:
BeginRequest
AuthenticateRequest
AuthorizeRequest
ResolveRequestCache
AquireRequestState
PreRequestHandlerExecute
PostRequestHandlerExecute
ReleaseRequestState
UpdateRequestCache
EndRequest

Both HttpModule and HttpHandler are loaded and attached to the call chain in part of the HttpApplication. Init () function call.

HttpApplication itself knows nothing about the data sent to the application-it is just a message object that communicates through events. it triggers an event and transmits a message to the called function through the HttpContext object. the actual status data of the current request is maintained by the HttpContext object mentioned above. it provides all the request-specific data and keeps following the request from the beginning to the end of the pipeline.

Once the pipeline is started, HttpApplication starts to trigger events one by one as shown in figure 6. each event processor is triggered. If an event is mounted, these processors execute their own tasks. the main task of this processing is to ultimately call the HttpHandler that receives the specific request. the processor (handler) is ASP. the core processing mechanism of NET requests is usually the place where all application-level code is executed. remember ASP. the NET page and Web Service Framework are both implemented as HttpHandler, which is also the core of request processing. A module tends to be a preprocessing or post-processor that is passed to the context of the processor (handler. ASP. typical default processors in. NET include pre-processing authentication, caching, and various encoding Mechanisms in post-processing.

Although HttpModule looks like an ISAPI filter, they all check that each of them passes through ASP. NET application requests, but they only check the ing to a single specific ASP.. NET application or virtual directory requests, that is, only requests mapped to ASP can be checked. NET Request. in this way, you can check all ASPX pages or any other ing to ASP. NET extension.

Implementing an HTTP module is very simple: you must implement the IHttpModule interface that contains two functions (Init () and Dispose. the passed event parameters include references to the HTTPApplication object, which gives you the ability to access the HttpContext object. you can mount the HTTP application event to these methods. for example, if you want to mount the AuthenticateRequest event to a module, you have time to thoroughly understand each object.

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.