Pengantar. Tentang apa artikel ini.
Belum lama ini di Habré, saya melihat sebuah artikel dengan judul yang menjanjikan "Apa itu kelas Startup dan Program.cs di ASP.NET Core"... Saya selalu tertarik dan tertarik pada apa yang sebenarnya terjadi di bawah kap perpustakaan atau kerangka kerja tertentu yang kebetulan saya gunakan untuk bekerja. Dan ini sepenuhnya berlaku untuk aplikasi web di ASP.NET Core. Dan saya berharap mendapatkan beberapa informasi baru dari artikel ini tentang cara kerja kelas yang disebutkan saat memulai aplikasi semacam itu. Artikel itu, sayangnya, mengecewakan saya: artikel itu hanya mengulangi satu bagian manual sekali lagi, saya tidak menerima informasi baru dari sana. Dan saat membacanya, saya berpikir bahwa mungkin ada orang lain yang, seperti saya, tertarik tidak hanya untuk mengetahui cara menggunakan framework ini atau itu (ASP.NET Core dalam hal ini), tetapi juga cara kerjanya. Dan karena saya, karena berbagai alasan, baru-baru ini mempelajari struktur internal ASP.NET Core cukup dalam, saya pikir,bahwa sekarang saya memiliki banyak hal untuk diceritakan tentang dia di luar cakupan manual. Dan itulah mengapa saya memutuskan untuk memulai dengan menulis artikel tentang apa sebenarnya kelas Startup dan Program - agar tidak banyak tahu tentang cara menggunakannya (ini ada di banyak manual, yang menurut saya, ada tidak ada gunanya menduplikasi), tetapi terutama tentang bagaimana kelas-kelas ini bekerja, dan - dalam konteks seluruh aplikasi web di ASP.NET Core. Namun, karena besarnya tidak dapat dipahami, subjek artikel ini dibatasi. Pertama-tama, ini terbatas pada cerita hanya tentang aplikasi web yang dibuat menggunakan jenis template aplikasi baru - Host Generik. Kedua, artikel hanya akan dikhususkan untuk bagaimana inisialisasi aplikasi web berlangsung,karena peran utama dari kelas yang dimaksud adalah persis seperti itu - inisialisasi dan peluncuran aplikasi yang dihosting. Jadi, kepada siapa topik yang sedang dipertimbangkan, bahkan dalam volume yang terbatas, menarik - selamat datang di bawah cat.
Pengantar. Kelanjutan
Untuk memulainya, saya akan memperluas bagian pengantar artikel sedikit - karena tidak semua yang ingin saya tulis bisa muat sebelum kata. Tapi, pertama-tama - ringkasan artikel (di bawah spoiler):
TL; DR
(Saya segera memperingatkan Anda: tidak ada semuanya di sini).
Inisialisasi aplikasi yang dibuat sesuai dengan template Generic Host terlihat seperti ini.
Objek Host Builder dibuat yang mengimplementasikan antarmuka IHostBuilder
Kemudian tahap konfigurasi dijalankan, di mana komponen aplikasi dikonfigurasi - baik dengan memanggil metode antarmuka IHostBuilder secara langsung, atau dengan memanggil metode ekstensi untuk antarmuka tersebut - metode statis yang ditentukan di kelas lain. Metode ekstensi ini, biasanya, melakukan konfigurasi dengan memanggil semua metode yang sama dari antarmuka IHostBuilder.
IHostBuilder -. , ( IHostBuilder). Generic Host ( - , Host).
(), IHost, Build IHostBuilder .
. : , , , , (options). IHost Build.
, . : ; ; ; - .
, 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, , .
:
-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, - . , ( ), . , .
:
ServiceDescriptor. , .
- , . - . - , , , . - IEnumerable -, , - IEnumerable IEnumerable, , . , IServiceCollection
. , (Singleton) ( ), - , , (Transient), (Scoped), () , , ServiceProvider IServiceScope CreateScope IServiceProvider. , , , .
:
) : , , ;
) -: , 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 , .
Dan di sanalah cerita tentang menginisialisasi aplikasi yang dibangun sesuai dengan template Generic Host dapat dianggap lengkap - sekarang aplikasi diluncurkan dan berjalan. Bukankah itu sangat sederhana? Luangkan waktu Anda, di kelanjutan artikel ini akan ada cerita tentang fitur inisialisasi aplikasi web - dan rasa kesederhanaan, jika ada, pasti akan memudar.
Lanjutan: akan segera diterbitkan. Itu sudah ditulis (dan bahkan diposting di blog saya, tetapi dalam bentuk yang tidak disisir lengkap).