您好,登錄后才能下訂單哦!
原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup
下文:
--Startup類
--Configure方法
--ConfigureServices方法
--可啟用的服務(wù)(Services Available in Startup)
--其他資源
Startup類配置請求管道,處理所有應(yīng)用程序接收的請求
The Startup
class configures the request pipeline that handles all requests made to the application.
Startup 類
asp.net core需要啟動類,通常以“Startup”命名,在程序WebHostBuilderExtensions中的UseStartup<TStartup>中指定Startup類名。
ASP.NET Core apps require a Startup
class. By convention, the Startup
class is named "Startup". You specify the startup class name in the Main
programs WebHostBuilderExtensions UseStartup<TStartup>
method.
你能分別定義Startup類在不同的環(huán)境(Environment),并在運(yùn)行時選擇適當(dāng)?shù)囊粋€啟動,如果在WebHost的配置或操作中指定了startupAssembly程序集,托管將加載startup程序集并查找Startup 或 Startup(Environment)類型,參考 StartupLoader 的 FindStartupType 和 Working with multiple environments,建議使用UseStartup<TStartup> .
You can define separate Startup
classes for different environments, and the appropriate one will be selected at runtime. If you specify startupAssembly
in the WebHost configuration or options, hosting will load that startup assembly and search for a Startup
or Startup[Environment]
type. See FindStartupType in StartupLoader
and Working with multiple environments. UseStartup<TStartup>
is the recommended approach.
Startup類構(gòu)造函數(shù)能接受通過依賴注入的依賴關(guān)系,你能用IHostingEnvironment設(shè)置配置,用ILoggerFactory設(shè)置logging提供程序。
The Startup
class constructor can accept dependencies that are provided through dependency injection. You can use IHostingEnvironment
to set up configuration sources and ILoggerFactory
to set up logging providers.
Startup類必須包含 方法 而 方法可選,兩個方法都在程序啟動時調(diào)用,這個類也可包括這些方法的特定環(huán)境版本。
The Startup
class must include a Configure
method and can optionally include a ConfigureServices
method, both of which are called when the application starts. The class can also include environment-specific versions of these methods.
了解關(guān)于在程序啟動時的異常處理
Learn about handling exceptions during application startup.
configure方法用于指定ASP.NET程序如何響應(yīng)HTTP請求,通過將中間件組件添加到由依賴注入提供的IApplicationBuilder實例中來配置請求管道。
The Configure
method is used to specify how the ASP.NET application will respond to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder
instance that is provided by dependency injection.
下面是來自默認(rèn)網(wǎng)站模板的示例,對管道增加一些擴(kuò)展方法用于支持 BrowserLink, error pages, static files, ASP.NET MVC, and Identity.
In the following example from the default web site template, several extension methods are used to configure the pipeline with support for BrowserLink, error pages, static files, ASP.NET MVC, and Identity
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); } else{ app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseIdentity(); app.UseMvc(routes =>{ routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
每個擴(kuò)展方法添加一個中間件組件到請求管道中,例如,UseMvc擴(kuò)展方法添加routing中間件到請求管道中并配置將MVC做為默認(rèn)處理
Each Use
extension method adds a middleware component to the request pipeline. For instance, the UseMvc
extension method adds the routing middleware to the request pipeline and configures MVCas the default handler.
關(guān)于IApplicationBuilder的詳細(xì)信息見 中間件。
For more information about how to use IApplicationBuilder
, see Middleware.
也可以在方法簽名中指定一些其他服務(wù),如 IHostingEnvironment 和 ILoggerFactory,在這種情況下如果他們可用將被注入這些服務(wù)。
Additional services, like IHostingEnvironment
and ILoggerFactory
may also be specified in the method signature, in which case these services will be injected if they are available.
ConfigureServices方法是可選的,但若調(diào)用將在 Configure 之前被調(diào)用(一些功能會在鏈接到請求管道之前添加上),配置操作在方法中設(shè)置。
The ConfigureServices method is optional; but if used, it's called before the Configure
method by the runtime (some features are added before they're wired up to the request pipeline). Configuration options are set in this method.
對于需要大量設(shè)置的功能,用 IServiceCollection的 Add[Service]擴(kuò)展方法,下面是默認(rèn)網(wǎng)站模板示例,將 Entity Framework, Identity, and MVC配置到程序中。
For features that require substantial setup there are Add[Service]
extension methods on IServiceCollection. This example from the default web site template configures the app to use services for Entity Framework, Identity, and MVC.
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); }
向服務(wù)容器中添加服務(wù),使他們可以通過依賴注入在你的程序中使用。
Adding services to the services container makes them available within your application via dependency injection.
ASP.NET Core 依賴注入 是在 程序的startup中 提供程序服務(wù),你能請求這些服務(wù)通過 Startup類的構(gòu)造函數(shù) 或 它的Configure和 ConfigureServices 發(fā)放中 將適當(dāng)?shù)慕涌谧鳛橐粋€參數(shù)傳入。
ASP.NET Core dependency injection provides application services during an application's startup. You can request these services by including the appropriate interface as a parameter on your Startup
class's constructor or one of its Configure
or ConfigureServices
methods.
按調(diào)用順序看 Startup類中的每個方法,服務(wù)作為一個參數(shù)被請求
Looking at each method in the Startup
class in the order in which they are called, the following services may be requested as parameters:
In the constructor: IHostingEnvironment
, ILoggerFactory
In the ConfigureServices
method: IServiceCollection
In the Configure
method: IApplicationBuilder
, IHostingEnvironment
, ILoggerFactory
, IApplicationLifetime
Working with Multiple Environments
Middleware
Logging
Configuration
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。