Application Startup in ASP.NET Core
By Steve Smith and Tom Dykstra
The Startup
class configures the request pipeline that handles all requests made to the application.
The Startup class
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.
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.
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.
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.
The Configure method
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.
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?}");
});
}
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 MVC as the default handler.
For more information about how to use IApplicationBuilder
, see Middleware.
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.
The ConfigureServices method
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.
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.
Services Available in Startup
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.
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