asp.net core MVC analysis: Start process __.net

Source: Internet
Author: User

asp.net core MVC is Microsoft's Open source Cross-platform MVC Framework, first of all, compared to the original MVC, the biggest difference is cross-platform, and then add some very useful new features, such as Taghelper,viewcomponent, Dependencyinjection and so on, now began asp.net core MVC analysis of the trip.

Any application has an entry point, as is the case with MVC, in the MVC program created by the new framework, There is a special file Program.cs, which contains a main method, which I believe everyone is familiar with, and the console application has a similar main method, which is actually the approach to the MVC procedure, so let's take a look at the main work of this method.

public static void Main (string[] args)
   {
       var host = new Webhostbuilder ()
           . Usekestrel ()
           . Usecontentroot (Directory.GetCurrentDirectory ())
           . Useiisintegration ()
           . Usestartup<startup> ()
           . Build ();

       Host. Run ();
   }

From the code above we can see that a Iwebhost object is build through the Webhostbuilder class, and then the host is called. The Run method completes the application startup. So let's take a look at how webhost was created. A trace of the Webhostbuilder class was finally found in the hosting project, with a few key codes in the build method:

Create an application-dependent injected Iservicecollection object, which will be explained separately to the var hostingservices = Buildhostingservices ();
var hostingcontainer = Hostingservices.buildserviceprovider (); Instantiate the Webhost object
var host = new Webhost (hostingservices, Hostingcontainer, _options, _config);//Initialize
host. Initialize ();

Find the Initialize () method in the Webhost class, in which the buildapplication completes the construction of the HTTP request processing pipeline. Let's take a look at the process of building a process pipeline.

This method is actually the Configureservices method in the startup class in the calling program, and completes the service dependent registration
ensureapplicationservices ();
Iserver related Operation
Ensureserver ();

var builderfactory = _applicationservices.getrequiredservice<iapplicationbuilderfactory> ();
var builder = Builderfactory.createbuilder (server.features);
Builder. ApplicationServices = _applicationservices;

var startupfilters = _applicationservices.getservice<ienumerable<istartupfilter>> ();
Action<iapplicationbuilder> Configure = _startup. Configure;
foreach (var filter in Startupfilters.reverse ())
{
//Call the Configure method in the Startup class to register the processing middleware (middleware)
    Configure = filter. Configure (Configure);
}

Configure (builder);
Return builder. Build ();

Here to finally understand the role of Startup.cs class. Once the basic configuration is complete, the application is ready to start.

Webhost.run method definition is in webhostextensions, as the extension method exists, the Run method calls the Webhost start method, the Webhost start method directly calls the Iserver Start method to start the service

This method is actually the Configureservices method in the startup class in the calling program, and completes the service dependent registration
ensureapplicationservices ();
Iserver related Operation
Ensureserver ();

var builderfactory = _applicationservices.getrequiredservice<iapplicationbuilderfactory> ();
var builder = Builderfactory.createbuilder (server.features);
Builder. ApplicationServices = _applicationservices;

var startupfilters = _applicationservices.getservice<ienumerable<istartupfilter>> ();
Action<iapplicationbuilder> Configure = _startup. Configure;
foreach (var filter in Startupfilters.reverse ())
{
//Call the Configure method in the Startup class to register the processing middleware (middleware)
    Configure = filter. Configure (Configure);
}

Configure (builder);
Return builder. Build ();

Here to finally understand the role of Startup.cs class. Once the basic configuration is complete, the application is ready to start.

Webhost.run method definition is in webhostextensions, as the extension method exists, the Run method calls the Webhost start method, the Webhost start method directly calls the Iserver Start method to start the service

Server.start (New Hostingapplication (_application, _logger, Diagnosticsource, httpcontextfactory));

_application:http Request processing Pipeline

Httpcontextfactory:httpcontext Factory, each HTTP request corresponds to a HttpContext object, this HttpContext is created by this factory, which is in the Httpabstractions project.

Request listening is initiated in the Iserver startup method, and when the HTTP request comes in, the Createcontext of the hostingapplication (ihttpapplication type) is first invoked to create the HttpContext object. Createcontext will rely on the Processrequestasync method mentioned above to invoke hostingapplication when it is created httpcontextfactory,httpcontext the request processing:

The public Task processrequestasync
{return
  _application. HttpContext);
}

The Processrequestasync method is simple enough to put HTTP requests directly into the HTTP processing pipeline for processing.

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.

Tags Index: