原文:連接web
Startup.cs的做用:
api
配置各服務和HTTP請求管道。
安全
ASP.NET Core中使用按慣例Startup
命名的類Startup.cs
:mvc
ConfigureServices
經過依賴注入(DI)或ApplicationServices在應用程序中使用。ConfigureServices
並Configure
在應用程序啓動時由ASP.NET Core runtime調用app
public class Startup { // Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ... } // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { ... } }
Startup在構建app host
時,將爲應用程序指定該類。app host這裏一般爲webHost在Program
類中的 CreateWebHostBuilder上調用時構建的。即調用WebHostBuilderExtensions.UseStartup <TSTARTUP>方法構建:ide
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }
HostingEnvironment 可做爲Startup
類構造函數參數服務,ConfigureServices用來添加其餘服務,在ConfigureServices中添加後,相應的服務和應用程序就能夠在Configure方法
中使用。函數
在類中依賴注入的常見用法Startup
是注入:工具
public class Startup { private readonly IHostingEnvironment _env; private readonly IConfiguration _config; private readonly ILoggerFactory _loggerFactory; public Startup(IHostingEnvironment env, IConfiguration config, ILoggerFactory loggerFactory) { _env = env; _config = config; _loggerFactory = loggerFactory; } public void ConfigureServices(IServiceCollection services) { var logger = _loggerFactory.CreateLogger<Startup>(); if (_env.IsDevelopment()) { // Development service configuration logger.LogInformation("Development environment"); } else { // Non-development service configuration logger.LogInformation($"Environment: {_env.EnvironmentName}"); } // Configuration is available during startup. // Examples: // _config["key"] // _config["subsection:suboption1"] } }
在startup.cs中ConfigureServices方法:ui
Configure
方法以前由Host調用。典型的模式是調用全部Add{Service}
方法,而後調用全部services.Configure{Service}
方法。請參閱配置身份服務。this
對於須要大量設置的功能,IServiceCollectionAdd{Service}
上有擴展方法。典型的ASP.NET Core應用程序會配置Entity Framework,Identity和MVC註冊服務:
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<IdentityUser>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); }
IApplicationBuilder
可用於該Configure
方法,但它未在服務容器中註冊。託管建立IApplicationBuilder
並直接傳遞給Configure
。在ASP.NET核心模板配置與支持的管道:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(); }
每一個Use
擴展方法都將一個或多箇中間件組件添加到請求管道。例如,UseMvc
擴展方法將Routing Middleware添加到請求管道並將MVC配置爲默認處理程序。
請求管道中的每一箇中間件組件負責調用管道中的下一個組件或者在適當的時候使鏈路短路。若是中間件鏈中沒有發生短路,則每一箇中間件都有第二次機會在請求發送到客戶端以前處理該請求。
其餘服務(例如IHostingEnvironment
和ILoggerFactory
)也能夠在Configure
方法簽名中指定。指定後,若是可用,則會注入其餘服務。
有關如何使用IApplicationBuilder
和中間件處理順序的更多信息,請參閱ASP.NET核心中間件。