Application Startup

By Steve Smith

ASP.NET Core provides complete control of how individual requests are handled by your application. The Startup class is the entry point to the application, setting up configuration and wiring up services the application will use. Developers configure a request pipeline in the Startup class that is used to handle all requests made to the application.

The Startup class

In ASP.NET Core, the Startup class provides the entry point for an application, and is required for all applications. It’s possible to have environment-specific startup classes and methods (see Working with Multiple Environments), but regardless, one Startup class will serve as the entry point for the application. ASP.NET searches the primary assembly for a class named Startup (in any namespace). You can specify a different assembly to search using the Hosting:Application configuration key. It doesn’t matter whether the class is defined as public; ASP.NET will still load it if it conforms to the naming convention. If there are multiple Startup classes, this will not trigger an exception. ASP.NET will select one based on its namespace (matching the project’s root namespace first, otherwise using the class in the alphabetically first namespace).

The Startup class can optionally accept dependencies in its constructor that are provided through dependency injection. Typically, the way an application will be configured is defined within its Startup class’s constructor (see Configuration). The Startup class must define a Configure method, and may optionally also define a ConfigureServices method, which will be called when the application is started.

The Configure method

The Configure method is used to specify how the ASP.NET application will respond to individual HTTP requests. At its simplest, you can configure every request to receive the same response. However, most real-world applications require more functionality than this. More complex sets of pipeline configuration can be encapsulated in middleware and added using extension methods on IApplicationBuilder.

Your Configure method must accept an IApplicationBuilder parameter. Additional services, like IHostingEnvironment and ILoggerFactory may also be specified, in which case these services will be injected by the server if they are available. In the following example from the default web site template, you can see several extension methods are used to configure the pipeline with support for BrowserLink, error pages, static files, ASP.NET MVC, and Identity.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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();

    // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Each Use extension method adds middleware 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.

You can learn all about middleware and using IApplicationBuilder to define your request pipeline in the Middleware topic.

The ConfigureServices method

Your Startup class can optionally include a ConfigureServices method for configuring services that are used by your application. The ConfigureServices method is a public method on your Startup class that takes an IServiceCollection instance as a parameter and optionally returns an IServiceProvider. The ConfigureServices method is called before Configure. This is important, because some features like ASP.NET MVC require certain services to be added in ConfigureServices before they can be wired up to the request pipeline.

Just as with Configure, it is recommended that features that require substantial setup within ConfigureServices be wrapped up in extension methods on IServiceCollection. You can see in this example from the default web site template that several Add[Something] extension methods are used to configure the app to use services from Entity Framework, Identity, and MVC:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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. Just as the Startup class is able to specify dependencies its methods require as parameters, rather than hard-coding to a specific implementation, so too can your middleware, MVC controllers and other classes in your application.

The ConfigureServices method is also where you should add configuration option classes, like AppSettings in the example above, that you would like to have available in your application. See the Configuration topic to learn more about configuring options.

Services Available in Startup

ASP.NET Core provides certain application services and objects during your application’s startup. You can request certain sets of these services by simply including the appropriate interface as a parameter on your Startup class’s constructor or one of its Configure or ConfigureServices methods. The services available to each method in the Startup class are described below. The framework services and objects include:

IApplicationBuilder
Used to build the application request pipeline. Available only to the Configure method in Startup. Learn more about Request Features.
IApplicationEnvironment
Provides access to the application properties, such as ApplicationName, ApplicationVersion, and ApplicationBasePath. Available to the Startup constructor and Configure method.
IHostingEnvironment
Provides the current EnvironmentName, WebRootPath, and web root file provider. Available to the Startup constructor and Configure method.
ILoggerFactory
Provides a mechanism for creating loggers. Available to the Startup constructor and Configure method. Learn more about Logging.
IServiceCollection
The current set of services configured in the container. Available only to the ConfigureServices method, and used by that method to configure the services available to an application.

Looking at each method in the Startup class in the order in which they are called, the following services may be requested as parameters:

Startup Constructor - IApplicationEnvironment - IHostingEnvironment - ILoggerFactory

ConfigureServices - IServiceCollection

Configure - IApplicationBuilder - IApplicationEnvironment - IHostingEnvironment - ILoggerFactory

注解

Although ILoggerFactory is available in the constructor, it is typically configured in the Configure method. Learn more about Logging.