5.6 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|---|
App startup in ASP.NET Core | rick-anderson | Learn how the Startup class in ASP.NET Core configures services and the app's request pipeline. | >= aspnetcore-3.1 | riande | mvc | 5/5/2023 | fundamentals/startup |
App startup in ASP.NET Core
:::moniker range=">= aspnetcore-7.0"
ASP.NET Core apps created with the web templates contain the application startup code in the Program.cs
file.
The following app startup code supports:
Apps using EventSource can measure the startup time to understand and optimize startup performance. The ServerReady
event in xref:Microsoft.AspNetCore.Hosting?displayProperty=fullName represents the point where the server is ready to respond to requests.
For more information on application startup, see xref:fundamentals/index.
Extend Startup with startup filters
Use xref:Microsoft.AspNetCore.Hosting.IStartupFilter:
- To configure middleware at the beginning or end of an app's middleware pipeline without an explicit call to
Use{Middleware}
. UseIStartupFilter
to add defaults to the beginning of the pipeline without explicitly registering the default middleware.IStartupFilter
allows a different component to callUse{Middleware}
on behalf of the app author. - To create a pipeline of
Configure
methods. IStartupFilter.Configure can set a middleware to run before or after middleware added by libraries.
IStartupFilter
implements xref:Microsoft.AspNetCore.Hosting.StartupBase.Configure%2A, which receives and returns an Action<IApplicationBuilder>
. An xref:Microsoft.AspNetCore.Builder.IApplicationBuilder defines a class to configure an app's request pipeline. For more information, see Create a middleware pipeline with IApplicationBuilder.
Each IStartupFilter
can add one or more middlewares in the request pipeline. The filters are invoked in the order they were added to the service container. Filters may add middleware before or after passing control to the next filter, thus they append to the beginning or end of the app pipeline.
The following example demonstrates how to register a middleware with IStartupFilter
. The RequestSetOptionsMiddleware
middleware sets an options value from a query string parameter:
The RequestSetOptionsMiddleware
is configured in the RequestSetOptionsStartupFilter
class:
The IStartupFilter
is registered in Program.cs
:
When a query string parameter for option
is provided, the middleware processes the value assignment before the ASP.NET Core middleware renders the response:
Middleware execution order is set by the order of IStartupFilter
registrations:
-
Multiple
IStartupFilter
implementations may interact with the same objects. If ordering is important, order theirIStartupFilter
service registrations to match the order that their middlewares should run. -
Libraries may add middleware with one or more
IStartupFilter
implementations that run before or after other app middleware registered withIStartupFilter
. To invoke anIStartupFilter
middleware before a middleware added by a library'sIStartupFilter
:- Position the service registration before the library is added to the service container.
- To invoke afterward, position the service registration after the library is added.
Note: You can't extend the ASP.NET Core app when you override Configure
. For more informaton, see this GitHub issue.
Add configuration at startup from an external assembly
An xref:Microsoft.AspNetCore.Hosting.IHostingStartup implementation allows adding enhancements to an app at startup from an external assembly outside of the app's Program.cs
file. For more information, see xref:fundamentals/configuration/platform-specific-configuration.
Startup, ConfigureServices, and Configure
For information on using the xref:Microsoft.AspNetCore.Hosting.StartupBase.ConfigureServices%2A and xref:Microsoft.AspNetCore.Hosting.StartupBase.Configure%2A methods with the minimal hosting model, see:
:::moniker-end