From Asp .net to Asp core (first article), "recalled Asp .net lifecycle and pipeline mechanism"

From 2016 Microsoft acquired in Xamarin integrated into Visual Studio and open up to now been more than three years from now .net core 1.0 to 2.2, and the upcoming 3.0, we saw that the road is Microsoft's cross-platform more go farther into the current Three students from the previous partial science students, I hope that the future will get better and better

As a Microsoft avid powder, from the end of 17 began to keen to learn and use .net core, the following simple talk about my understanding of the framework asp core of the web

1: First, the first question .net core and Asp core What is the difference? Is the same thing?

  .net core is a unified platform, Microsoft is ready to be used to develop the mobile side, desktop applications, web sites, etc. of the overall platform

  asp core is .net core focus on developing web sites framework, which also is like the relationship before the .net framework and Aps .net framework, it can be understood to include relations

2: Asp core with the original Asp .net Framework What is the difference, which in the end compared to the traditional Asp .net Framework What are the benefits?

  First of all I want to say, Asp core actually compare the original Asp .net, in fact, can be said to be entirely new set of things to Asp .net Asp .net MVC, Asp .net MVC before (just not as in the original Asp .net framework to expand and upgrade its nature or the .net framework that set).

But although Asp core is a new framework hear that is something new and do not stress, a lot of personal feeling based on the development of the use of Asp core, still retains some of the original .net developer to develop the habit, the actual development process personal the feeling is not giving a strange feeling

  Comparing the difference between Asp core of the Asp .net:

  • First, a Asp core is cross-platform, what is cross-platform, is Asp core can be deployed and run on another server than as described windows (when of course must be installed on the system you want to run have run, like the java JRE) such as more suitable for server systems linux server, but not like before Aps .net totally dependent on IIS, the new Asp core can be deployed to IIS, Nginx, Apache and other proxy server
  • The new Asp.net core Kestrel default Http request as a listener, and does not rely on the original large and complex Https.sys. Kestrel is not just the next generation of Microsoft's cross-platform Http request listeners, while also providing a more lightweight and faster than Https.sys Http request processing.
  • Asp.net core design with the original Web Another big difference is that Asp.net core (and .net core), completely abandoned the use of the original pipeline mode to receive and process HttpRequest. Asp.net core allows the processing middleware (the Middleware) to be a request for all HttpRequest, when a request is received, Asp.net core middleware calls registered (in the order of registration) of HttpRequest processing. Doing so in comparison with the original use of HttpApplication pipeline manner, its advantage is entirely up to the developer how to HttpRequest need to perform processing, no extra additional steps. The original way even if developers do not want a HttpRequest any treatment, but still will in turn create HttpApplication pipeline according to the settings of HttpModel -> activate HttpHandler -> Session to deal with and so on. Relative to the original Aps .net, program performance is also greatly improved

3: a frame of the traditional process flow asp .net

  3.1 IIS server request processing flow

  1, when the IIS Http server receives a request, it is for IIS, it relies on built-in drivers called HTTP.SYS to listen for HTTP requests from the outside.

  When the operating system starts, IIS first register their virtual path in HTTP.SYS. In fact, the equivalent of telling HTTP.SYS which URL is accessible, which is not accessible (Here is a simple example: Why do you access the file does not exist it will be 404 error occurs is determined in this step?).

  If the request is a URL accessible, HTTP.SYS forward this request to the IIS worker process ( IIS6.0 in is called w3wp.exe, IIS5.0 in is called aspnet_wp.exe. ).

  Each worker process has an identity and a range of optional performance parameters (such as setting refers to the collection mechanism, timeout, etc.).

  2, then the request will be passed to the first ISAPI (Internet Server Application Programe Interface, Internet Server Application Programming Interface), ISAPI decide how to deal with this request, if the static file server to obtain the requested (can also be a file, such as jimmy.jpg) after the extension, the next will look at the application server can handle this type of file extension, if you can not find the IIS processing applications such files, and the file has not been protected by the server (such as code files, .config configuration file are protected, unprotected, such as js, css), then IIS will return the file directly to the client.

What ISAPI that? Long-sawed in the end?

ISAPI Server Extensions can be loaded HTTP server and call DLL (dynamic link library)

Figure:

  3, then the ISAPI need HttpRuntime ProcessRequest method calls, enter request pipeline cycle begins, the final request cycle through the whole conduit, ISAPI receives the data stream returned again returned to the HTTP.SYS and, finally, the data and then the HTTP.SYS returned to the client browser.

The above text summary may be some boring, perhaps many will be following this picture clear

 Process: Request Start => Http.Sys => ISAPI => CLR / HttpRuntime => ISAPI => Http.Sys => end of the request

  3.2 understand pipeline

  Spoke in front of a simple request process in IIS, but talked about the details of when the request comes after asp.net Runtime, only in passing, in the end did not request process through which the asp.net Runtime, the end is how the return request the result, following on to briefly explain the whole process

  Here it would be related to the more important in the .net framework two concepts: Http Module and  Http Handler

  Want to understand these two concepts, we must first know the experience in the following figure after when the request comes in asp.net Runtime nineteen events

For this event to be a few nonsense, this process although looking more for beginners in fact do not have too much pressure, in fact, no need to research each one, but he was probably over again, knowing that there is such a thing, mixed with a look like

Http Module Managed Module

If we want a particular event in the above framework to deal with our own code, such as we have to be screened to the requests when they reach a certain event or a log record at this time, how should we do?

Here it is necessary to use Http Module, we can request pipeline (Pipeline) via Http Http Module in the desired registration method to respond to application events

Http Module implements IHttpModule interfaces, IHttpModule If the interface and achieve the Init method Dispose

Dispose: it can be tidied up before it is garbage collected.

Init: we can register any of the above nineteen events in one or more of the init method, and to do the actual processing logic in the corresponding callback method

Specific code as follows:

    public class TestModule : IHttpModule
    {
        void IHttpModule.Dispose()
        {
        }

        void IHttpModule.Init(HttpApplication context)
        {
            context.EndRequest += new EventHandler(context_EndRequest);
        }

        private void context_EndRequest(object sender, EventArgs e)
        {
            / * The HttpApplication object contains all the required context object requests, such as HttpContext, HttpRequest, HttpResponse, HttpSession, etc., so we can pass this object
             To complete the request filtering, logging, etc. will be used in the actual development function
             */
            HttpApplication application = (HttpApplication)sender;

            HttpContext context = application.Context;
            context.Response.Write ( " <HR> <style = h1 of 'Color: # F00'> from HttpModule process, the end of the request </ h1 of> " );
        }
    }

It is noteworthy that the request is usually handled Http Module is global, application level, not only for what a page, what does that mean, for example, the code above as an example, the effect of the code above will make all the pages will have "come from HttpModule processing request end ", this sentence

Http Handler Handler

Contrast Http Module, authority to process the request HttpHandler is relatively low, a finer dimensions, it can only page for a specific suffix, in fact, our webform pages in fact, is a Handler

All aspx page inherits System.Web.UI.Page, you can see the Page class also implements the interface IHttpHandler

public interface IHttpHandler{
    void ProcessRequest(HttpContext context);
    bool IsReusable { get; }
}
  • ProcessRequest processing request for the main code.
  • IsReusable property, is explained on MSDN: Gets a value indicating whether another request can use IHttpHandler instance. That is not the successor of Http request can continue to use implements this interface instance of the class, in general, I set it to true.

 We HttpHandler used more scenes that have Ajax + Httphandler for web page without refreshing the development and realization of the picture is the HttpHandler anti-theft chain, IhttpHandler achieve image verification code and so on, programming and traditional Http Module HttpHandler, in fact, are relatively old development mode, and personally feel the need to learn and understand .net has only these two things just fine

After the above-based learning, we have to look after total entered asp.net Runtime, and Http Module and HttpHandler what happened relation to tease out a process

  1. Request to enter HttpRuntime
  2. HttpRuntime HttpApplication object is created by HttpApplicationFactory (HttpApplication represents the Web application programmers created .HttpApplication create HttpContext object for this Http requests, these objects contain many other objects on this request as HttpRequest, HttpResponse, HttpSessionState)
  3. Next Http request for the application level through a series of processing requests Module
  4. After processing is complete Http Module will request to the HttpHandler to do some customization of page-level processing
  5. HttpHandler in the back after treatment is completed Http Module will do some follow-up of the finishing process

 Asp.Net above is the whole life cycle, although after that, there was Asp.Net MVC, as I said before, but Asp.Net MVC framework is not a new framework, Asp.net Mvc or in Asp.net run-time basis. But Asp Core and no longer Asp.netRuntime -based, so it does not nineteen events, nor Http Module and HttpHandler this set, then I will simply parse the next article requested period of Asp Core

 

References:

http://go.microsoft.com/?linkid=8101007

https://docs.microsoft.com/en-us/previous-versions/aspnet/ms227435(v=vs.100)

http://www.tracefact.net/tech/001.html

https://www.cnblogs.com/me-sa/archive/2009/06/01/MVCLifecycle.html

Guess you like

Origin www.cnblogs.com/ruanraun/p/apsnet.html