ProgramクラスとStartupクラスについて-ASP.NETアプリケーションの初期化。パートI:プログラムとIHostBuilder

前書き。この記事は何についてです。

少し前にHabréで、「ASP.NETCoreのスタートアップクラスとProgram.csとは」という有望なタイトルの記事を見ました。..。私はいつも、私がたまたま一緒に仕事をしている特定のライブラリやフレームワークの内部で何が起こっているのかに興味を持っていました。そして、これはASP.NETCore上のWebアプリケーションに完全に適用されます。そして、このようなアプリケーションを起動するときに、前述のクラスがどのように機能するかについて、この記事からいくつかの新しい情報を取得したいと思っていました。その記事は、残念ながら私を失望させました。それは、マニュアルの一部をもう一度繰り返しただけで、そこから新しい情報を受け取ることはありませんでした。そして、それを読んでいる間、私と同じように、このフレームワークまたはそのフレームワーク(この場合はASP.NET Core)の使用方法だけでなく、その動作方法にも興味を持っている人がおそらくいると思いました。そして、私はさまざまな理由で、最近ASP.NET Coreの内部構造を非常に深く掘り下げたので、次のように思いました。今では、マニュアルの範囲を超えて彼について話すことがたくさんあります。そのため、StartupクラスとProgramクラスが実際に何であるかについての記事を書くことから始めることにしました-それらの使用方法についてはあまり説明しません(これは多くのマニュアルにありますが、私にはそう思われますが、 )を複製する意味はありませんが、主にこれらのクラスがどのように機能するかについて、そして-ASP.NETCore上のWebアプリケーション全体のコンテキストで。ただし、広大さは把握できないため、本稿の主題は限定的です。まず第一に、それは新しいタイプのアプリケーションテンプレートであるGenericHostを使用して作成されたWebアプリケーションについての話に限定されています。次に、この記事では、Webアプリケーションの初期化がどのように行われるかについてのみ説明します。問題のクラスの主な役割はまさにそれであるためです-ホストされたアプリケーションの初期化と起動。ですから、このように限られた量であっても、検討中のトピックが興味深いのは、猫の下で歓迎します。





.

- , . , - ( ):





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





:





図。 1.汎用ホスト初期化プロセスの図
. 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 , .





ここで、汎用ホストテンプレートに従って構築されたアプリケーションの初期化に関するストーリーが完了したと見なすことができます。これで、アプリケーションが起動され、実行されます。本当に簡単ではないですか?時間をかけて、この記事の続きで、Webアプリケーションを初期化する機能についての話があります-そして、もしあれば、単純さの感覚は確実に消えていきます。





継続:まもなく公開されます。それはすでに書かれています(そして私のブログにも投稿されていますが、不完全にまとめられた形で)。








All Articles