You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ASP.NET Core templates create a .NET Core Generic Host (<xref:Microsoft.Extensions.Hosting.HostBuilder>).
16
+
This article provides information on using the .NET Generic Host in ASP.NET Core.
17
17
18
-
This article provides information on using .NET Generic Host in ASP.NET Core. For information on using .NET Generic Host in console apps, see [.NET Generic Host](/dotnet/core/extensions/generic-host).
18
+
The ASP.NET Core templates create a <xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder> and <xref:Microsoft.AspNetCore.Builder.WebApplication>, which provide a streamlined way to configure and run web applications without a `Startup` class. For more information on `WebApplicationBuilder` and `WebApplication`, see <xref:migration/50-to-60#new-hosting-model>.
19
+
20
+
For information on using the .NET Generic Host in console apps, see [.NET Generic Host](/dotnet/core/extensions/generic-host).
19
21
20
22
## Host definition
21
23
@@ -28,65 +30,17 @@ A *host* is an object that encapsulates an app's resources, such as:
28
30
29
31
When a host starts, it calls <xref:Microsoft.Extensions.Hosting.IHostedService.StartAsync%2A?displayProperty=nameWithType> on each implementation of <xref:Microsoft.Extensions.Hosting.IHostedService> registered in the service container's collection of hosted services. In a web app, one of the `IHostedService` implementations is a web service that starts an [HTTP server implementation](xref:fundamentals/index#servers).
30
32
31
-
The main reason for including all of the app's interdependent resources in one object is lifetime management: control over app startup and graceful shutdown.
33
+
Including all of the app's interdependent resources in one object enables control over app startup and graceful shutdown.
32
34
33
35
## Set up a host
34
36
35
-
The host is typically configured, built, and run by code in the `Program` class. The `Main` method:
36
-
37
-
* Calls a `CreateHostBuilder` method to create and configure a builder object.
38
-
* Calls `Build` and `Run` methods on the builder object.
39
-
40
-
The ASP.NET Core web templates generate the following code to create a host:
37
+
The host is typically configured, built, and run by code in the *Program.cs*. The following code creates a host with an `IHostedService` implementation added to the DI container:
The following code creates a non-HTTP workload with an `IHostedService` implementation added to the DI container.
41
+
For an HTTP workload, call <xref:Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.ConfigureWebHostDefaults%2A> after <xref:Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder%2A>:
If the app uses Entity Framework Core, don't change the name or signature of the `CreateHostBuilder` method. The [Entity Framework Core tools](/ef/core/miscellaneous/cli/) expect to find a `CreateHostBuilder` method that configures the host without running the app. For more information, see [Design-time DbContext Creation](/ef/core/miscellaneous/cli/dbcontext-creation).
@@ -131,11 +85,17 @@ For more information on framework-provided services, see <xref:fundamentals/depe
131
85
132
86
## IHostApplicationLifetime
133
87
134
-
Inject the <xref:Microsoft.Extensions.Hosting.IHostApplicationLifetime> (formerly `IApplicationLifetime`) service into any class to handle post-startup and graceful shutdown tasks. Three properties on the interface are cancellation tokens used to register app start and app stop event handler methods. The interface also includes a `StopApplication` method.
88
+
Inject the <xref:Microsoft.Extensions.Hosting.IHostApplicationLifetime> (formerly `IApplicationLifetime`) service into any class to handle post-startup and graceful shutdown tasks. Three properties on the interface are cancellation tokens used to register app start and app stop event handler methods. The interface also includes a `StopApplication` method, which allows apps to request a graceful shutdown.
135
89
136
-
The following example is an `IHostedService` implementation that registers `IHostApplicationLifetime` events:
* Triggers the <xref:Microsoft.Extensions.Hosting.IHostApplicationLifetime.ApplicationStopping%2A> event handlers, which allows the app to run logic before the shutdown process begins.
93
+
* Stops the server, which disables new connections. The server waits for requests on existing connections to complete, for as long as the [shutdown timeout](#shutdowntimeout) allows. The server sends the connection close header for further requests on existing connections.
94
+
* Triggers the <xref:Microsoft.Extensions.Hosting.IHostApplicationLifetime.ApplicationStopped%2A> event handlers, which allows the app to run logic after the application has shutdown.
95
+
96
+
The following example is an `IHostedService` implementation that registers `IHostApplicationLifetime` event handlers:
@@ -271,15 +223,7 @@ Some host settings apply only to HTTP workloads. By default, environment variabl
271
223
272
224
Extension methods on `IWebHostBuilder` are available for these settings. Code samples that show how to call the extension methods assume `webBuilder` is an instance of `IWebHostBuilder`, as in the following example:
@@ -397,13 +327,8 @@ The assembly to search for the `Startup` class.
397
327
398
328
To set this value, use the environment variable or call `UseStartup`. `UseStartup` can take an assembly name (`string`) or a type (`TStartup`). If multiple `UseStartup` methods are called, the last one takes precedence.
@@ -495,36 +414,6 @@ Call methods on the built <xref:Microsoft.Extensions.Hosting.IHost> implementati
495
414
496
415
<xref:Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.WaitForShutdownAsync%2A> returns a <xref:System.Threading.Tasks.Task> that completes when shutdown is triggered via the given token and calls <xref:Microsoft.Extensions.Hosting.IHost.StopAsync%2A>.
497
416
498
-
### External control
499
-
500
-
Direct control of the host lifetime can be achieved using methods that can be called externally:
0 commit comments