Skip to content

Commit d43b809

Browse files
Merge pull request #24244 from dotnet/main
Merge to Live
2 parents ccc350c + a440c98 commit d43b809

File tree

71 files changed

+74960
-298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+74960
-298
lines changed

aspnetcore/blazor/host-and-deploy/index.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ Publish locations:
5252

5353
The assets in the folder are deployed to the web server. Deployment might be a manual or automated process depending on the development tools in use.
5454

55+
## IIS Application Pools
56+
57+
Sharing an app pool among ASP.NET Core apps isn't supported, including for Blazor apps. Use one app pool per app when hosting with IIS, and avoid the use of IIS's [virtual directories](/iis/get-started/planning-your-iis-architecture/understanding-sites-applications-and-virtual-directories-on-iis#virtual-directories) for hosting multiple apps.
58+
59+
One or more Blazor WebAssembly apps hosted by an ASP.NET Core app, known as a [hosted Blazor WebAssembly solution](xref:blazor/hosting-models#blazor-webassembly), are supported for ***one*** app pool. However, we don't recommend or support assigning a single app pool to multiple hosted Blazor WebAssembly solutions or in sub-app hosting scenarios. For more information, see <xref:host-and-deploy/iis/advanced#sub-applications>.
60+
5561
## App base path
5662

5763
The *app base path* is the app's root URL path. Consider the following ASP.NET Core app and Blazor sub-app:
@@ -211,6 +217,12 @@ Publish locations:
211217

212218
The assets in the folder are deployed to the web server. Deployment might be a manual or automated process depending on the development tools in use.
213219

220+
## IIS Application Pools
221+
222+
Sharing an app pool among ASP.NET Core apps isn't supported, including for Blazor apps. Use one app pool per app when hosting with IIS, and avoid the use of IIS's [virtual directories](/iis/get-started/planning-your-iis-architecture/understanding-sites-applications-and-virtual-directories-on-iis#virtual-directories) for hosting multiple apps.
223+
224+
One or more Blazor WebAssembly apps hosted by an ASP.NET Core app, known as a [hosted Blazor WebAssembly solution](xref:blazor/hosting-models#blazor-webassembly), are supported for ***one*** app pool. However, we don't recommend or support assigning a single app pool to multiple hosted Blazor WebAssembly solutions or in sub-app hosting scenarios. For more information, see <xref:host-and-deploy/iis/advanced#sub-applications>.
225+
214226
## App base path
215227

216228
The *app base path* is the app's root URL path. Consider the following ASP.NET Core app and Blazor sub-app:
@@ -350,6 +362,12 @@ Publish locations:
350362

351363
The assets in the folder are deployed to the web server. Deployment might be a manual or automated process depending on the development tools in use.
352364

365+
## IIS Application Pools
366+
367+
Sharing an app pool among ASP.NET Core apps isn't supported, including for Blazor apps. Use one app pool per app when hosting with IIS, and avoid the use of IIS's [virtual directories](/iis/get-started/planning-your-iis-architecture/understanding-sites-applications-and-virtual-directories-on-iis#virtual-directories) for hosting multiple apps.
368+
369+
One or more Blazor WebAssembly apps hosted by an ASP.NET Core app, known as a [hosted Blazor WebAssembly solution](xref:blazor/hosting-models#blazor-webassembly), are supported for ***one*** app pool. However, we don't recommend or support assigning a single app pool to multiple hosted Blazor WebAssembly solutions or in sub-app hosting scenarios. For more information, see <xref:host-and-deploy/iis/advanced#sub-applications>.
370+
353371
## App base path
354372

355373
The *app base path* is the app's root URL path. Consider the following ASP.NET Core app and Blazor sub-app:

aspnetcore/fundamentals/host/generic-host.md

Lines changed: 36 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ description: Use .NET Core Generic Host in ASP.NET Core apps. Generic Host is r
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 4/17/2020
8+
ms.date: 11/09/2021
99
no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: fundamentals/host/generic-host
1111
---
1212
# .NET Generic Host in ASP.NET Core
1313

1414
:::moniker range=">= aspnetcore-6.0"
1515

16-
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.
1717

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).
1921

2022
## Host definition
2123

@@ -28,65 +30,17 @@ A *host* is an object that encapsulates an app's resources, such as:
2830

2931
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).
3032

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.
3234

3335
## Set up a host
3436

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:
4138

42-
```csharp
43-
public class Program
44-
{
45-
public static void Main(string[] args)
46-
{
47-
CreateHostBuilder(args).Build().Run();
48-
}
49-
50-
public static IHostBuilder CreateHostBuilder(string[] args) =>
51-
Host.CreateDefaultBuilder(args)
52-
.ConfigureWebHostDefaults(webBuilder =>
53-
{
54-
webBuilder.UseStartup<Startup>();
55-
});
56-
}
57-
```
39+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Program.cs" id="snippet_Host":::
5840

59-
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>:
6042

61-
```csharp
62-
public class Program
63-
{
64-
public static void Main(string[] args)
65-
{
66-
CreateHostBuilder(args).Build().Run();
67-
}
68-
69-
public static IHostBuilder CreateHostBuilder(string[] args) =>
70-
Host.CreateDefaultBuilder(args)
71-
.ConfigureServices((hostContext, services) =>
72-
{
73-
services.AddHostedService<Worker>();
74-
});
75-
}
76-
```
77-
78-
For an HTTP workload, the `Main` method is the same but `CreateHostBuilder` calls `ConfigureWebHostDefaults`:
79-
80-
```csharp
81-
public static IHostBuilder CreateHostBuilder(string[] args) =>
82-
Host.CreateDefaultBuilder(args)
83-
.ConfigureWebHostDefaults(webBuilder =>
84-
{
85-
webBuilder.UseStartup<Startup>();
86-
});
87-
```
88-
89-
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).
43+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_HostConfigureWebHostDefaults":::
9044

9145
## Default builder settings
9246

@@ -131,11 +85,17 @@ For more information on framework-provided services, see <xref:fundamentals/depe
13185

13286
## IHostApplicationLifetime
13387

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.
13589

136-
The following example is an `IHostedService` implementation that registers `IHostApplicationLifetime` events:
90+
When performing a graceful shutdown, the host:
13791

138-
:::code language="csharp" source="generic-host/samples-snapshot/3.x/LifetimeEventsHostedService.cs" id="snippet_LifetimeEvents":::
92+
* 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:
97+
98+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Services/HostApplicationLifetimeEventsHostedService.cs" id="snippet_Class":::
13999

140100
## IHostLifetime
141101

@@ -168,7 +128,7 @@ The environment variable provider with prefix `DOTNET_` and command-line argumen
168128

169129
The following example creates host configuration:
170130

171-
:::code language="csharp" source="generic-host/samples-snapshot/3.x/Program.cs" id="snippet_HostConfig":::
131+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_ConfigureHostConfiguration":::
172132

173133
## App configuration
174134

@@ -206,11 +166,7 @@ The <xref:Microsoft.Extensions.Hosting.IHostEnvironment.ContentRootPath%2A?displ
206166

207167
To set this value, use the environment variable or call `UseContentRoot` on `IHostBuilder`:
208168

209-
```csharp
210-
Host.CreateDefaultBuilder(args)
211-
.UseContentRoot("c:\\content-root")
212-
//...
213-
```
169+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_UseContentRoot":::
214170

215171
For more information, see:
216172

@@ -228,11 +184,7 @@ The <xref:Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName%2A?displ
228184

229185
To set this value, use the environment variable or call `UseEnvironment` on `IHostBuilder`:
230186

231-
```csharp
232-
Host.CreateDefaultBuilder(args)
233-
.UseEnvironment("Development")
234-
//...
235-
```
187+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_UseEnvironment":::
236188

237189
### ShutdownTimeout
238190

@@ -250,7 +202,7 @@ If the timeout period expires before all of the hosted services stop, any remain
250202

251203
To set this value, use the environment variable or configure `HostOptions`. The following example sets the timeout to 20 seconds:
252204

253-
:::code language="csharp" source="generic-host/samples-snapshot/3.x/Program.cs" id="snippet_HostOptions":::
205+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_ShutdownTimeout":::
254206

255207
### Disable app configuration reload on change
256208

@@ -271,15 +223,7 @@ Some host settings apply only to HTTP workloads. By default, environment variabl
271223

272224
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:
273225

274-
```csharp
275-
public static IHostBuilder CreateHostBuilder(string[] args) =>
276-
Host.CreateDefaultBuilder(args)
277-
.ConfigureWebHostDefaults(webBuilder =>
278-
{
279-
webBuilder.CaptureStartupErrors(true);
280-
webBuilder.UseStartup<Startup>();
281-
});
282-
```
226+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_ConfigureWebHostDefaults":::
283227

284228
### CaptureStartupErrors
285229

@@ -292,9 +236,7 @@ When `false`, errors during startup result in the host exiting. When `true`, the
292236

293237
To set this value, use configuration or call `CaptureStartupErrors`:
294238

295-
```csharp
296-
webBuilder.CaptureStartupErrors(true);
297-
```
239+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderCaptureStartupErrors":::
298240

299241
### DetailedErrors
300242

@@ -307,9 +249,7 @@ When enabled, or when the environment is `Development`, the app captures detaile
307249

308250
To set this value, use configuration or call `UseSetting`:
309251

310-
```csharp
311-
webBuilder.UseSetting(WebHostDefaults.DetailedErrorsKey, "true");
312-
```
252+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderDetailedErrors":::
313253

314254
### HostingStartupAssemblies
315255

@@ -322,9 +262,7 @@ A semicolon-delimited string of hosting startup assemblies to load on startup. A
322262

323263
To set this value, use configuration or call `UseSetting`:
324264

325-
```csharp
326-
webBuilder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "assembly1;assembly2");
327-
```
265+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderHostingStartupAssemblies":::
328266

329267
### HostingStartupExcludeAssemblies
330268

@@ -337,9 +275,7 @@ A semicolon-delimited string of hosting startup assemblies to exclude on startup
337275

338276
To set this value, use configuration or call `UseSetting`:
339277

340-
```csharp
341-
webBuilder.UseSetting(WebHostDefaults.HostingStartupExcludeAssembliesKey, "assembly1;assembly2");
342-
```
278+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderHostingStartupExcludeAssemblies":::
343279

344280
### HTTPS_Port
345281

@@ -352,9 +288,7 @@ The HTTPS redirect port. Used in [enforcing HTTPS](xref:security/enforcing-ssl).
352288

353289
To set this value, use configuration or call `UseSetting`:
354290

355-
```csharp
356-
webBuilder.UseSetting("https_port", "8080");
357-
```
291+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderHttpsPort":::
358292

359293
### PreferHostingUrls
360294

@@ -367,9 +301,7 @@ Indicates whether the host should listen on the URLs configured with the `IWebHo
367301

368302
To set this value, use the environment variable or call `PreferHostingUrls`:
369303

370-
```csharp
371-
webBuilder.PreferHostingUrls(false);
372-
```
304+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderPreferHostingUrls":::
373305

374306
### PreventHostingStartup
375307

@@ -382,9 +314,7 @@ Prevents the automatic loading of hosting startup assemblies, including hosting
382314

383315
To set this value, use the environment variable or call `UseSetting` :
384316

385-
```csharp
386-
webBuilder.UseSetting(WebHostDefaults.PreventHostingStartupKey, "true");
387-
```
317+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderPreventHostingStartup":::
388318

389319
### StartupAssembly
390320

@@ -397,13 +327,8 @@ The assembly to search for the `Startup` class.
397327

398328
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.
399329

400-
```csharp
401-
webBuilder.UseStartup("StartupAssemblyName");
402-
```
403-
404-
```csharp
405-
webBuilder.UseStartup<Startup>();
406-
```
330+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderUseStartup":::
331+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderUseStartupGeneric":::
407332

408333
### SuppressStatusMessages
409334

@@ -416,9 +341,7 @@ When enabled, suppresses hosting startup status messages.
416341

417342
To set this value, use configuration or call `UseSetting`:
418343

419-
```csharp
420-
webBuilder.UseSetting(WebHostDefaults.SuppressStatusMessagesKey, "true");
421-
```
344+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderSuppressStatusMessages":::
422345

423346
### URLs
424347

@@ -431,9 +354,7 @@ A semicolon-delimited list of IP addresses or host addresses with ports and prot
431354

432355
To set this value, use the environment variable or call `UseUrls`:
433356

434-
```csharp
435-
webBuilder.UseUrls("http://*:5000;http://localhost:5001;https://hostname:5002");
436-
```
357+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderUseUrls":::
437358

438359
Kestrel has its own endpoint configuration API. For more information, see <xref:fundamentals/servers/kestrel/endpoints>.
439360

@@ -448,9 +369,7 @@ The [IWebHostEnvironment.WebRootPath](xref:Microsoft.AspNetCore.Hosting.IWebHost
448369

449370
To set this value, use the environment variable or call `UseWebRoot` on `IWebHostBuilder`:
450371

451-
```csharp
452-
webBuilder.UseWebRoot("public");
453-
```
372+
:::code language="csharp" source="generic-host/samples/6.x/GenericHostSample/Snippets/Program.cs" id="snippet_WebHostBuilderUseWebRoot":::
454373

455374
For more information, see:
456375

@@ -495,36 +414,6 @@ Call methods on the built <xref:Microsoft.Extensions.Hosting.IHost> implementati
495414

496415
<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>.
497416

498-
### External control
499-
500-
Direct control of the host lifetime can be achieved using methods that can be called externally:
501-
502-
```csharp
503-
public class Program
504-
{
505-
private IHost _host;
506-
507-
public Program()
508-
{
509-
_host = new HostBuilder()
510-
.Build();
511-
}
512-
513-
public async Task StartAsync()
514-
{
515-
_host.StartAsync();
516-
}
517-
518-
public async Task StopAsync()
519-
{
520-
using (_host)
521-
{
522-
await _host.StopAsync(TimeSpan.FromSeconds(5));
523-
}
524-
}
525-
}
526-
```
527-
528417
:::moniker-end
529418

530419
:::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using GenericHostSample.Services;
2+
3+
// <snippet_Host>
4+
await Host.CreateDefaultBuilder(args)
5+
.ConfigureServices(services =>
6+
{
7+
services.AddHostedService<SampleHostedService>();
8+
})
9+
.Build()
10+
.RunAsync();
11+
// </snippet_Host>

0 commit comments

Comments
 (0)