About the Program and Startup classes - initializing an ASP.NET application. Part I: Program and IHostBuilder

Introduction. What is this article about.

Not so long ago on Habré, I saw an article with a promising title "What is the Startup class and Program.cs in ASP.NET Core"... I have always been interested and interested in what exactly happens under the hood of this or that library or framework with which I happen to work. And this fully applies to web applications on ASP.NET Core. And I was hoping to get some new information from this article about how the mentioned classes work when starting such an application. That article, unfortunately, disappointed me: it only reiterated a piece of the manual once again, I did not receive any new information from there. And while reading it, I thought that there are probably other people who, like me, are interested not only in knowing how to use this or that framework (ASP.NET Core in this case), but also in how it works. And since, for various reasons, I have recently delved into the internal structure of ASP.NET Core quite deeply, I thought,that now I have a lot to tell about him beyond the scope of the manuals. And that's why I decided to start by writing an article about what the Startup and Program classes really are - so as to tell not so much about how to use them (this is in numerous manuals, which, as it seems to me, there is no point in duplicating ), but mainly about how these classes work, and - in the context of the entire web application on ASP.NET Core. However, since the immensity cannot be grasped, the subject of this article is limited. First of all, it is limited to a story only about web applications created using a new type of application template - Generic Host. Secondly, the article will be devoted only to how the initialization of the web application takes place,because the main role of the classes in question is exactly that - initializing and running the hosted application. So, to whom the topic under consideration, even in such a limited volume, is interesting - welcome under cat.





Introduction. Continuation

To begin with, I will expand the introductory part of the article a little - because not everything I wanted to write about there fit before the kata. But, first of all - a summary of the article (under the spoiler):





TL; DR

(I warn you right away: there is not everything here).





The initialization of an application made according to the Generic Host template looks like this.





  1. A Host Builder object is created that implements the IHostBuilder interface





  2. Then the configuration stage is executed, in which the application components are configured - either by calling methods of the IHostBuilder interface directly, or by calling extension methods for this interface - static methods defined in other classes. These extension methods, as a rule, perform configuration by calling all the same methods of the IHostBuilder interface.





  3. IHostBuilder -. , ( IHostBuilder). Generic Host ( - , Host).





  4. (), IHost, Build IHostBuilder .





  5. . : , , , , (options). IHost Build.





  6. , . : ; ; ; - .





  7. , StartAsync IHost. - IHostedService - StartAsync. , .





.





, , , . , :





:

ASP.NET Core - - , . , , , - . , - , , , . , , " ", . , , : ASP.NET. : - , , , , ( , ). : , -, Generic Host, .. - - : , , , , .





:

, : , , - ... - - - : , .. " ". ( ) ( ) .





. ASP.NET Core . , . , Visual Studio , ASP.NET Core ", , " . ASP.NET Core .





:

, : , ASP.NET Core ( ) , , /, , , (" "), , ( - -) ( - , , - -), - , - , , var, ( - ) , / / - , C#. , , , - , ;-). (-) .





, ASP.NET, - ;-) , GOTO DO 5 . , -, , , , , , , ;-) (, , , - , ). .





ASP.NET Core, , Generic Host, - (, , - . ), , - .





:

, , - , - " ". D "SOLID". : " ", " ". - . , , , : , , , , , , - : . , , - , , . , . .





( ) . , , .

.





:

, , . : , , - // - - IMHO . IDE, , , : , , , , , . , , ( , - ).





, ASP.NET, .





, , ASP.NET - , , , , " ": , , , , . .





, ASP.NET, Generic Host , -: Web Host. (Microsoft) ASP.NET Core ( 3.1.8) , . , , , , . Generic Host. , -, (Middleware), , : .





, , (, IHost), .NET Core, ( - , DI Container), (Configuration), (Options).





, - . , , . , - , , " " ( ) . , , , ( ), , -, , ( ), , -, , , - , - , , - . , , Middleware, ( ASP.NET) , .





...

, - . , , , ASP.NET program.cs Program, Main. , Visual Studio, , . , CreateHostBuilder: CreateDefaultBuilder Microsoft.Extension.Hosting.Host , IHostBuilder ( ), IHostBuilder IHostBuilder ( ), , , . CreateHostBuilder , , ORM Entity Framework Core (DbContext), ( ). , Main, IHostBuilder.Build , IHost. , , IHost : Run, , , . - .





: ?

- . , , , ASP.NET Core "" (Builder pattern). , IMHO , , ( , ). - , Builder pattern Dependency Injection ( ), ASP.NET Core - . .





. , - (// ), , . , - , ( - -) - .





-: ConfigureWebHostDefaults -, Startup ( , -, , , , Startup-).





.. - Startup-, , , Startup- - : -, , Startup-. , Startup- - .





: -.

ConfigureWebHost, , -, . , IDE() ( ) : , ( ) IWebHostBuilder. - . , , , , - .





, - - , - , -.





. , - -





,

( - .NET Framework 3.5) (Expression Trees) - , . -, , , , . "" ;-) .





, , - . , ASP.NET Core " ", " ".





:

: , . , : , - ( Startup-, ). , , : ( StackOverflow ..), , , . . " ": , -, , , , . , , , - , , - ! (, , , ): , , , - (), . (, ) : " ( ) ". , , . : , , , , , . , , , , .





:

, , , , Generic Host, , .





:





Fig.  1. Diagram of the Generic Host initialization process
. 1. Generic Host

-c - , , - , . , . , , . - , . . - : , . . - , . - / ( , , ). - .





Host.CreateHostBuilder , IHostBuilder. () AddFeature1..AddFeatureN. IHostBuilder , Build. Build , . IHost ( - , Host) StartAsync .





IHostBuilder, CreateDefaultBuilder (, Microsoft.Extensions.Hosting.Host), Microsoft.Extensions.Hosting.HostBuilder( , - IHostBuilder). CreateDefaultBuilder , ( . ). - - , new, , , " ".





, ..

, , . , , , , . : ". ". - . , - , - , , .





Build. , , IHost ( "") .





-

Host, . Microsoft "" , - "node" "", . - Host "", " ". - - "" - , , -. .





, - "" - - ( ) , , . List<>, ,





:

( ) - , , (, , ).





, : , , , . .1 (""), : . , IHostBuilder . .1 , . - , .





: IHostBuilder

IHostBuilder, . IHostBuilder, UseServiceProviderFactory ( ) -, - , IServiceProvider. . , , IHostBuilder ( , , ) - Properties( IDictionary<object,object>), , ( - , ). , , IHostBuilder . (, ): HostBuilderContext, - ( ), (Singleton) .





- IHostBuilder. IHostBuilder . - , - .





Generic Host

, , , Generic Host





ASP.NET Core ( .NET Core ) - , IConfiguration .NET Core -, -, .





: ,

( , IConfigurationProvider) .NET Core : (environment), , ... , IConfigurationProvider. - : (, ) . , (, ), , . - .. : - , . , , , , , IConfiguration. ( - ) : , : (Bind) .





. : - IConfigurationBuilder - IConfigurationSource. - Build - IConfiguration. - , .





, , , ASP.NET Core .NET Core - : IServiceProvider. , , , . , - , , , . ( ) GetService -, - .





IServiceCollection, - . , ( ), . , .





:
  1. ServiceDescriptor. , .





  2. - , . - . - , , , . - IEnumerable -, , - IEnumerable IEnumerable, , . , IServiceCollection





  3. . , (Singleton) ( ), - , , (Transient), (Scoped), () , , ServiceProvider IServiceScope CreateScope IServiceProvider. , , , .





  4. :





    ) : , , ;





    ) -: , IServiceProvider; ( ); - ;





    ) ( ): .





ASP.NET Core .NET Core ( - ) - (Options). , , , , () , , . , -, , , .NET, : , , , , , . ( , - IOptions<>, IOptionsSnapshot<>, IOptionsMonitor<>, ). -, , , - (options). -, , . . , (options), , . IServiceCollection, . , , IConfiguration ( - ), , -, . , .





, - ( ), IHost , Build IHostBuilder. Generic Host Microsoft.Extensions.Hosting.HostBuilder ( HostBuilder) Build .





Build. Build .





: Build

HostBuilder.Build , . HostBuilder _hostBuilt, false. , true, - InvalidOperationException. _hostBuilt true.





, , . , - , - - , - , , , , . , (Host Configuration, - ). - , ( ), , , . - . . , , ConfigureHostConfiguration IHostBuilder _configureHostConfigActions. .1 "" 1.





:

ConfigureHostConfiguration -, - IConfigurationBuilder.





BuildHostConfiguration().





:

- ConfigurationBuider( IConfigurationBuilder) - configBuilder( .1 ) - , ( ) - ( IConfiguration) , . , IConfigurationBuilder ( , , ). , , ( IConfigurationBuilder.Build()) ( - ).





_hostConfiguration .





- , , . - (), IHostEnvironment. CreateHostingEnvironment(). HostingEnvironment, IHostEnvironment IHostingEnvironment ( IHostEnvironment). ( - . ). (ApplicationName), (Environment) (ContentRootPath).





- (-, . ). , , ContentRootFileProvider PhysicalFileProvider ContentRootPath - , ( ) . _hostingEnvironment .





, - ( HostBuilderContext) CreateHostBuilderContext. , : Properties - - ( . " : IHostBuilder"), Environment - (IHostEnvironment), Configuration - () ( ). _hostBuilderContext. , , .





BuildAppConfiguration. , , ConfigureAppConfiguration IHostBuilder _configureAppConfigActions. .1 "" 2.





:

ConfigureAppConfiguration -, - HostBuilderContext IHostBuilder. - ConfigurationBuider( IConfigurationBuilder) IHostEnvironment.ContentRootPath . IConfigurationBuilder SetBasePath. "FileProvider" PhysicalPathProvider . IFileProvider - (- FileConfigurationSource) , , - - , - .





IConfigurationBuilder . .





:

ChainedConfigurationSource, , ChainedConfigurationSource , ( Dispose) ChainedConfigurationProvider, .





.





:

IConfigurationBuilder _configureAppConfigActions, . - . . IConfigurationBuilder.Build.





( IConfiguration) _appConfiguration .





, IHostBuilder.Build - . CreateServiceProvider. , , .





- , . IServiceProviderFactory. , , , -, , , . - , , , .. , : ( . ) , IServiceCollection . - - ContainerBuilder( -, T), Container. "-". , - : .NET Core , , , , . ASP.NET Core .





, , IServiceProviderFactory. , CreateBuilder, IServiceCollection services) -, ( ) . CreateServiceProvider - - ( IServiceProvider).





(IHost) IHostBuilder UseServiceProviderFactory ( - -), : , - , HostBuilderContext , , . , , ( .1 ).





:

, , _serviceProviderFactory, - IConfigureContainerAdapter. - , HostBuilder, , -, - - . IConfigureContainerAdapter IServiceProviderFactory, . - , CreateBuilder - HostBuilderContext: , , UseServiceProviderFactory. - , IConfigureContainerAdapter - Object. , , ConfigureContainerAdapter, ( ) - - UseServiceProviderFactory, , , CreateServiceProvider . -, UseServiceProviderFactory , - (. ) (- ).





DefaultServiceProviderFactory. - IServiceCollection, .. IServiceProviderFactory. - - ( - , ).





:

ServiceProviderOptions - (options) . -: ValidateScopes - , (Scoped) ( ) ValidateOnBuild - , . ServiceProviderOptions.Default, . , , .





CreateBuilder DefaultServiceProviderFactory, , . CreateServiceProvider BuildServiceProvider IServiceCollection.





: CreateServiceProvider

, BuildServiceProvider, IServiceCollection, CreateServiceProvider - "-". ServiceProviderOptions - .





IServiceProvider. , , .





CreateServiceProvider , . IServiceCollection - services( .1 ). () , .





:

, - ServiceCollection. : List ( ), IServiceCollection . - ( (Singleton) ). - - , , Build: IHostEnvironment IHostingEnvironment ( - HostingEnvironment _hostingEnvironment) HostBuilderContext ( , ) ( - _hostBuilderContext. -, , , . - , , , ( ): IHostLifetime ( - ConsoleLifetime), IHostApplicationLifetime ( - ApplicationLifetime) IApplicationLifetime ( , IHostApplicationLifetime - ApplicationLifetime - , IApplicationLifetime) IConfiguration - -, - _appConfiguration ( -). - Dispose . - (. CreateServiceProvider ). , IServiceCollection , (options) - AddOptions, (logging) - AddLogging





( (Singleton) , Generic Host - IHost: Internal.Host, .





- - IServiceCollection _configureServicesActions. .1 "" 3. ConfigureServices. , : HostBuilderContext IServiceCollection.





- - containerBuilder ( .1 ).





: -

CreateBuilder IConfigureContainerAdapter, _serviceProviderFactory - containerBuilder





- - , , IServiceCollection.





- - - - - _configureContainerActions. .1 "" 4. ConfigureContainer. "" ( ) ASP.NET Core , , . , , , Startup- , , , ( UseStartup IWebHostBuilder).





( - )

ConfigureContainer - , -, -. - -, : - HostBuilderContext -, - -, - - ConfigureContainer. , - , ( List<>, ), IConfigureContainerAdapter, ConfigureContainerAdapter, -, ( - ). . ConfigureContainer , - Object. ConfigureContainerAdapter - . : : , - , - -. . , ConfigureContainer ConfigureContainerAdapter - , , , , Build . , , Object - - . IMHO - .





, - .





: -

- : (Scoped) , , Singleton - , , , , (Scoped). - , ( Development) - , InvalidOperationException. ValidateScopes ServiceProviderOptions, DefaultServiceProviderFactory, BuildServiceProvider IServiceCollection, . CreateBuilder IConfigureContainerAdapter, _serviceProviderFactory, - containerBuilder





CreateServiceProvider .





:

- : IConfiguration - , _appConfiguration ( Dispose()) ( - )





, Build HostBuilder - IHost ( Internal.Host), .





:

, IHost Build . Internal.Host, , -, , , , , , . , , , . - : - Build, . Internal.Host, HostBuilder.Build - , (option) HostOptions: , . , - - - Timespan. IHost HostBuilder.Build , .





, IHost - , . , IHost.StartAsync - , , StartAsync. StartAsync IHost - Internal.Host - ( ) IHostedService.StartAsync: IHostedService. . , - . , StartAsync IHost , .





And that's where the story about initializing an application built according to the Generic Host template can be considered complete - now the application is launched and running. Isn't it really very simple? Take your time, in the continuation of this article there will be a story about the features of initializing a web application - and the feeling of simplicity, if any, will surely fade away.





Continuation: will be published soon. It has already been written (and even posted on my blog, but in an incompletely combed form).








All Articles