[Dot NET Core](Graphic series )1. Host Outline diagram of Framework operation - With Startup Class

Czxdas
15 min readNov 30, 2021

--

ASP.NET Core

Net Core is a cross-platform application development, and many self-made services can be added to this framework, which is not an exaggeration to call it a container.

If you want to explain this framework in detail, complete and clear, you can write a whole thick book. But if you want to understand the main operation process of the framework first, especially with the MVC controller, you can sort out its trajectory in a graphical way, and make a relationship diagram as a guide for stepping into this framework. In the future, clues can be found for in-depth discussion.

The following is an overview diagram of a Dot NET Core project with MVC Controller:

First show the entry point and Startup category of the project:

Dot NET Core Simple Example — Main and Startup

Then, the entry point “Main” is used to carry out the related construction of the Host:

asp.net core startup host

First enter the first message CreateHostBuilder (), using the static category Host. Then the route of S1 adds the HostBuilder entity, which is a large container inside the framework, including several sub-containers, and other important fields such as _serviceProviderFactory, _appServices, etc.; there are also driver messages such as Build, CreateServiceProvider, etc. Along the S2 route, come to the expansion category of the HostBuilder entity, you can find the ConfugureWebHostDefaults message, which includes related settings such as Web Server, IIS, and related providers. After setting the given settings and pre-planning the program to be done, go to the CreateServiceProvider of the S3 route. Service registration will be performed here, so first, a ServiceCollection entity will be established in S4 as a container. Its extended category provides important registration letters Add, AddScoped, AddSingleton, AddTransient, etc. The registered services will be stored in _desciptors, which is a collection of ServiceDescriptor .

The next step is to continue what the HostBuilder entity’s ConfugureWebHostDefaults does:

asp.net core HostBuilder

The ConfugureWebHostDefaults of the HostBuilder entity will add a new entity GenericWebHostBuilder, and send this entity to the ConfugureWebDefaults message of the static category WebHost to do the environment settings, the registration of related services, the settings of IIS and the related construction program planning.

In addition, it will call the UseStartup message of GenericWebHostBuilder, it will initialize the settings through the expansion category under Microsoft.AspNetCore.Hosting, and then call the ConfigureServices message of GenericWebHostBuilder, which will first delegate content to HostBuilder.ConfigureServices; finally leave UseStartup and then Delegate to HostBuilder.ConfigureServices as follows:

HostBuilder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());

The above can be regarded as most of the setting and delegating program behavior. At this point, it is a halfway paragraph. Next is HostBuilder.Build() , which is very important. There are 5 major steps. The following is the first step, which is to call HostBuilder.BuildHostConfiguration:

asp.net core HostBuilder build 1

First add the entity ConfigurationBuilder, and use ConfigurationBuilder.Build to add the ConfigurationRoot entity, and save it back to the _hostConfiguration of the HostBuilder. Continue foreach to process the delegated content of the sub-container _configureHostConfigActions in HostBuilder. One of the content points is to execute GenericWebHostBuilder.ExecuteHosingStartups(), which generates the entity HostingStartupWebHostBuilder and stores it back to its own _hostingStartupWebHostBuilder, which can be used to do related transactions to GenericWebHostBuilder .

The second step is to call HostBuilder.CreateHostingEnvironment, which mostly sets the environment.

The third step is to call HostBuilder.CreateHostingBuilderContext, as follows:

asp.net core HostBuilder build 2

Added the HostBuilderContext entity, which stores the _hostConfiguration and _hostingEnvironment references of HostBuilder, and then saves it back to the _hostBuilderContext of HostBuilder.

The fourth step calls HostBuilder.BuildAppConfiguration:

asp.net core HostBuilder build 3

Added the ConfigurationBuilder entity, and also added the ChainedConfigurationSource entity with the augmented category message, added the _hostConfiguration reference of HostBuilder to the configuration property, and then saved the entire entity to ConfigurationBuilder.Sources; then called ConfigurationBuilder.Build to create a new entity. Add the entity ConfigurationRoot and save it back to the _appConfiguration of the HostBuilder.

The fifth step calls HostBuilder.CreateServiceProvider:

asp.net core HostBuilder build 4

​This step is closely related to the Startup category that developers will come into contact with. First, the container _configureServicesActions of HostBuilder will be processed by foreach. One of the processed transactions is to retrieve the Startup category in the project, and use the StartupLoader.FindMethod method to store the relevant information of the ConfigureServices function in the Startup category in the ConfigureServicesBuilder.MethodInfo property. Then execute this function.

This message can be used by developers to register related services, and can also register the implementation of DI to Controller constructor sub-parameters to the _descriptor of ServiceCollection.

Finally, using the preset ServiceFactoryAdapter and DefaultServiceProviderFactory, the Provider is established as follows:

asp.net core HostBuilder build 5

It takes the ServiceCollection as a parameter to generate the CallSiteFactory entity through the DynamicServiceProviderEngine and the parent class ServiceProviderEngine as a property, and saves it back to the _appServices of the HostBuilder. If you want to implement a registered service, you can call GetService in the ServiceProvider inherited by DynamicServiceProviderEngine to resolve.

Going back to the ConfigureServices function in the Startup category, this project executes services.AddControllers(), as follows:

asp.net core service add controller

Then execute:

services
.AddMvcCore()
.AddApiExplorer()
.AddAuthorization()
.AddCors()
.AddDataAnnotations()
.AddFormatterMappings()

services.AddMvcCore is the AddMvcCore function under the extended category of ServiceCollection, and finally generates the MvcBuilder entity, one of which is Services, which is the reference of the entire ServiceCollection. The purpose of this MvcBuilder entity is to integrate these functions in the form of MvcBuilder extended categories for the subsequent AddApiExplorer, AddAuthorization, AddCors, AddDataAnnotations and AddFormatterMappings. The purpose of these functions is to register the relevant service with the _descriptor in the ServiceCollection.

The last thing to show is the time point and purpose when the Configure function is called in the Startup category. The operation relationship is as follows:

asp.net core HostBuilder Run 2

In the stage of HostBuilder.Run(), one of the tasks is Host.StartAsync(), which will first execute the task of generating the ApplicationBuilder entity, as shown in the figure above. At this time, the GenericWebHostService.Options.ConfigureApplication is passed in, and then ConfigurationBuilder.Build is used to execute Startup Configure function in . This function can be used by developers to use the constructed ApplicaitonBuilder to add some MiddleWare and execute them in order.

The above is the interaction between Dot Net Core and the Startup category of the project when the Host is established and set up. First, establish a reference like a map guide for the introduction guide when you can further discuss in more detail in the future.

--

--

Czxdas
Czxdas

Written by Czxdas

Keep looking for Prajna wisdom

No responses yet