Asp.net core mvc analysis: startup process, coremvc
Asp.net core mvc is a Microsoft open-source cross-platform mvc Framework. First of all, it is different from the original MVC, that is, cross-platform, and then adds some very useful new functions, such as taghelper, viewcomponent, DependencyInjection, and so on, now start the asp.net core mvc analysis tour.
Any application has an entry point, as does MVC. In the MVC Program created through the new framework, there is a special file Program. cs, which contains a Main method. I believe this method is familiar to everyone. The console application also has a similar Main method, which is actually the entry method of the MVC program, let's take a look at the main work in 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 an IWebHost object is built through the WebHostBuilder class, and then the host. Run method is called to start the application. Let's take a look at how the WebHost is built. Finally, the WebHostBuilder class was found in the Hosting project. The Build method contains several key codes:
// Create an IServiceCollection object for application dependency injection. This will be explained separately later.
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, and call BuildApplication to construct the http request processing pipeline. Let's take a look at the process of building a processing pipeline.
// This method calls the ConfigureServices method in the Startup class in the program to complete service dependency registration.
EnsureApplicationServices ();
// IServer related operations 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 middleware (middleware) configure = filter. Configure (configure);} configure (builder );
Here, we finally understand the role of the startup. cs class. After the basic configuration is completed, the application can be started.
The WebHost. Run method is defined in WebHostExtensions and exists as an extension method. The Run method calls the Start method of WebHost. The Start method of WebHost directly calls the Start method of IServer 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 an httpcontext object. This httpcontext is created by this factory, which is in the httpmediaactions project.
In the IServer startup method, request listening is started. When an http request comes over, CreateContext of HostingApplication (IHttpApplication type) is called to create an HttpContext object. CreateContext will depend on httpContextFactory mentioned above, after HttpContext is created, the ProcessRequestAsync method of the HostingApplication will be called for request processing:
public Task ProcessRequestAsync(Context context){ return _application(context.HttpContext);}
The ProcessRequestAsync method is very simple. It directly puts the http request into the http processing pipeline for processing.
The next article will introduce how the KestrelServer listens for requests and submits them to the MPs queue for processing.