Informationen zu den Programm- und Startklassen - Initialisieren einer ASP.NET-Anwendung. Teil I: Programm und IHostBuilder

Einführung. Wovon handelt der Artikel.

Vor nicht allzu langer Zeit habe ich auf Habré einen Artikel mit dem vielversprechenden Titel "Was ist die Startup-Klasse und Program.cs in ASP.NET Core" gesehen.... Ich war schon immer interessiert und interessiert daran, was genau unter der Haube einer bestimmten Bibliothek oder eines bestimmten Frameworks passiert, mit dem ich gerade arbeite. Dies gilt vollständig für Webanwendungen unter ASP.NET Core. Und ich hatte gehofft, aus diesem Artikel einige neue Informationen darüber zu erhalten, wie die genannten Klassen beim Starten einer solchen Anwendung funktionieren. Dieser Artikel hat mich leider enttäuscht: Er hat nur einen Teil des Handbuchs noch einmal wiederholt, ich habe von dort keine neuen Informationen erhalten. Und als ich es las, dachte ich, dass es wahrscheinlich andere Leute gibt, die wie ich nicht nur daran interessiert sind, wie man dieses oder jenes Framework (in diesem Fall ASP.NET Core) verwendet, sondern auch, wie es funktioniert. Und da ich mich aus verschiedenen Gründen kürzlich eingehend mit der internen Struktur von ASP.NET Core befasst habe, dachte ich:dass ich jetzt viel über ihn zu erzählen habe, was über den Rahmen der Handbücher hinausgeht. Und deshalb habe ich beschlossen, zunächst einen Artikel darüber zu schreiben, was die Startup- und Program-Klassen wirklich sind - um nicht so viel darüber zu erzählen, wie man sie verwendet (dies steht in zahlreichen Handbüchern, die es meiner Meinung nach gibt Es macht keinen Sinn, sie zu duplizieren), sondern hauptsächlich darüber, wie diese Klassen funktionieren und - im Kontext der gesamten Webanwendung unter ASP.NET Core. Da die Unermesslichkeit jedoch nicht erfasst werden kann, ist das Thema dieses Artikels begrenzt. Erstens beschränkt es sich auf eine Geschichte nur über Webanwendungen, die mit einer neuen Art von Anwendungsvorlage erstellt wurden - Generic Host. Zweitens wird der Artikel nur der Art und Weise gewidmet, wie die Initialisierung der Webanwendung erfolgt.denn die Hauptaufgabe der betreffenden Klassen ist genau das - Initialisierung und Start der gehosteten Anwendung. Also, für wen das betreffende Thema auch in einem so begrenzten Umfang interessant ist - willkommen unter Katze.





.

- , . , - ( ):





TL;DR

( : - ).





, Generic Host .





  1. (Host), IHostBuilder





  2. , - IHostBuilder , - , . , , IHostBuilder.





  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, , .





:





Feige.  1. Diagramm des Generic Host-Initialisierungsprozesses
. 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 , .





Und hier kann die Geschichte über die Initialisierung einer Anwendung, die gemäß der Vorlage "Generic Host" erstellt wurde, als vollständig betrachtet werden - jetzt wird die Anwendung gestartet und ausgeführt. Ist es nicht wirklich sehr einfach? Nehmen Sie sich Zeit, in der Fortsetzung dieses Artikels finden Sie eine Geschichte über die Funktionen der Initialisierung einer Webanwendung - und das Gefühl der Einfachheit, falls vorhanden, wird sicherlich verschwinden.





Fortsetzung: wird in Kürze veröffentlicht. Es wurde bereits geschrieben (und sogar in meinem Blog veröffentlicht, jedoch in unvollständig gekämmter Form).








All Articles