原文:连接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核心中间件。