您好,登錄后才能下訂單哦!
原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup
下文:
--Startup類
--Configure方法
--ConfigureServices方法
--可啟用的服務(Services Available in Startup)
--其他資源
Startup類配置請求管道,處理所有應用程序接收的請求
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類在不同的環境(Environment),并在運行時選擇適當的一個啟動,如果在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類構造函數能接受通過依賴注入的依賴關系,你能用IHostingEnvironment設置配置,用ILoggerFactory設置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類必須包含 方法 而 方法可選,兩個方法都在程序啟動時調用,這個類也可包括這些方法的特定環境版本。
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.
了解關于在程序啟動時的異常處理
Learn about handling exceptions during application startup.
configure方法用于指定ASP.NET程序如何響應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.
下面是來自默認網站模板的示例,對管道增加一些擴展方法用于支持 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?}"); }); }
每個擴展方法添加一個中間件組件到請求管道中,例如,UseMvc擴展方法添加routing中間件到請求管道中并配置將MVC做為默認處理
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.
關于IApplicationBuilder的詳細信息見 中間件。
For more information about how to use IApplicationBuilder
, see Middleware.
也可以在方法簽名中指定一些其他服務,如 IHostingEnvironment 和 ILoggerFactory,在這種情況下如果他們可用將被注入這些服務。
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方法是可選的,但若調用將在 Configure 之前被調用(一些功能會在鏈接到請求管道之前添加上),配置操作在方法中設置。
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.
對于需要大量設置的功能,用 IServiceCollection的 Add[Service]擴展方法,下面是默認網站模板示例,將 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>(); }
向服務容器中添加服務,使他們可以通過依賴注入在你的程序中使用。
Adding services to the services container makes them available within your application via dependency injection.
ASP.NET Core 依賴注入 是在 程序的startup中 提供程序服務,你能請求這些服務通過 Startup類的構造函數 或 它的Configure和 ConfigureServices 發放中 將適當的接口作為一個參數傳入。
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.
按調用順序看 Startup類中的每個方法,服務作為一個參數被請求
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。