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
This article introduces the .NET Core Generic Host (<xref:Microsoft.Extensions.Hosting.HostBuilder>) and provides guidance on how to use it.
15
+
The ASP.NET Core templates create a .NET Core Generic Host (<xref:Microsoft.Extensions.Hosting.HostBuilder>).
16
16
17
-
## What's a host?
17
+
## Host definition
18
18
19
19
A *host* is an object that encapsulates an app's resources, such as:
20
20
@@ -27,16 +27,33 @@ When a host starts, it calls `IHostedService.StartAsync` on each implementation
27
27
28
28
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.
29
29
30
-
In versions of ASP.NET Core earlier than 3.0, the [Web Host](xref:fundamentals/host/web-host) is used for HTTP workloads. The Web Host is no longer recommended for web apps and remains available only for backward compatibility.
31
-
32
30
## Set up a host
33
31
34
32
The host is typically configured, built, and run by code in the `Program` class. The `Main` method:
35
33
36
34
* Calls a `CreateHostBuilder` method to create and configure a builder object.
37
35
* Calls `Build` and `Run` methods on the builder object.
38
36
39
-
Here's *Program.cs* code for a non-HTTP workload, with a single `IHostedService` implementation added to the DI container.
37
+
The ASP.NET Core web templates generate the following code to create a host:
The following code creates a non-HTTP workload with a `IHostedService` implementation added to the DI container.
40
57
41
58
```csharp
42
59
publicclassProgram
@@ -132,7 +149,7 @@ Inject the <xref:Microsoft.Extensions.Hosting.IHostEnvironment> service into a c
132
149
133
150
*[ApplicationName](#applicationname)
134
151
*[EnvironmentName](#environmentname)
135
-
*[ContentRootPath](#contentrootpath)
152
+
*[ContentRootPath](#contentroot)
136
153
137
154
Web apps implement the `IWebHostEnvironment` interface, which inherits `IHostEnvironment` and adds the [WebRootPath](#webroot).
138
155
@@ -175,7 +192,7 @@ The [IHostEnvironment.ApplicationName](xref:Microsoft.Extensions.Hosting.IHostEn
175
192
176
193
To set this value, use the environment variable.
177
194
178
-
### ContentRootPath
195
+
### ContentRoot
179
196
180
197
The [IHostEnvironment.ContentRootPath](xref:Microsoft.Extensions.Hosting.IHostEnvironment.ContentRootPath*) property determines where the host begins searching for content files. If the path doesn't exist, the host fails to start.
181
198
@@ -391,14 +408,14 @@ Kestrel has its own endpoint configuration API. For more information, see <xref:
391
408
392
409
### WebRoot
393
410
394
-
The relative path to the app's static assets.
411
+
The [IWebHostEnvironment.WebRootPath](xref:Microsoft.AspNetCore.Hosting.IWebHostEnvironment.WebRootPath) property determines the relative path to the app's static assets. If the path doesn't exist, a no-op file provider is used.
395
412
396
413
**Key**: `webroot`
397
414
**Type**: `string`
398
-
**Default**: The default is `wwwroot`. The path to *{content root}/wwwroot* must exist. If the path doesn't exist, a no-op file provider is used.
415
+
**Default**: The default is `wwwroot`. The path to *{content root}/wwwroot* must exist.
399
416
**Environment variable**: `<PREFIX_>WEBROOT`
400
417
401
-
To set this value, use the environment variable or call `UseWebRoot`:
418
+
To set this value, use the environment variable or call `UseWebRoot` on `IWebHostBuilder`:
*[Fundamentals: Web root](xref:fundamentals/index#web-root)
410
-
*[ContentRootPath](#contentrootpath)
427
+
*[ContentRoot](#contentroot)
411
428
412
429
## Manage the host lifetime
413
430
@@ -970,9 +987,9 @@ public class MyClass
970
987
971
988
::: moniker range=">= aspnetcore-5.0"
972
989
973
-
This article introduces the .NET Core Generic Host (<xref:Microsoft.Extensions.Hosting.HostBuilder>) and provides guidance on how to use it.
990
+
The ASP.NET Core templates create a .NET Core Generic Host (<xref:Microsoft.Extensions.Hosting.HostBuilder>).
974
991
975
-
## What's a host?
992
+
## Host definition
976
993
977
994
A *host* is an object that encapsulates an app's resources, such as:
978
995
@@ -985,16 +1002,33 @@ When a host starts, it calls `IHostedService.StartAsync` on each implementation
985
1002
986
1003
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.
987
1004
988
-
In versions of ASP.NET Core earlier than 3.0, the [Web Host](xref:fundamentals/host/web-host) is used for HTTP workloads. The Web Host is no longer recommended for web apps and remains available only for backward compatibility.
989
-
990
1005
## Set up a host
991
1006
992
1007
The host is typically configured, built, and run by code in the `Program` class. The `Main` method:
993
1008
994
1009
* Calls a `CreateHostBuilder` method to create and configure a builder object.
995
1010
* Calls `Build` and `Run` methods on the builder object.
996
1011
997
-
Here's *Program.cs* code for a non-HTTP workload, with a single `IHostedService` implementation added to the DI container.
1012
+
The ASP.NET Core web templates generate the following code to create a host:
The following code creates a non-HTTP workload with a `IHostedService` implementation added to the DI container.
998
1032
999
1033
```csharp
1000
1034
publicclassProgram
@@ -1090,7 +1124,7 @@ Inject the <xref:Microsoft.Extensions.Hosting.IHostEnvironment> service into a c
1090
1124
1091
1125
*[ApplicationName](#applicationname)
1092
1126
*[EnvironmentName](#environmentname)
1093
-
*[ContentRootPath](#contentrootpath)
1127
+
*[ContentRootPath](#contentroot)
1094
1128
1095
1129
Web apps implement the `IWebHostEnvironment` interface, which inherits `IHostEnvironment` and adds the [WebRootPath](#webroot).
1096
1130
@@ -1133,7 +1167,7 @@ The [IHostEnvironment.ApplicationName](xref:Microsoft.Extensions.Hosting.IHostEn
1133
1167
1134
1168
To set this value, use the environment variable.
1135
1169
1136
-
### ContentRootPath
1170
+
### ContentRoot
1137
1171
1138
1172
The [IHostEnvironment.ContentRootPath](xref:Microsoft.Extensions.Hosting.IHostEnvironment.ContentRootPath*) property determines where the host begins searching for content files. If the path doesn't exist, the host fails to start.
1139
1173
@@ -1362,14 +1396,14 @@ Kestrel has its own endpoint configuration API. For more information, see <xref:
1362
1396
1363
1397
### WebRoot
1364
1398
1365
-
The relative path to the app's static assets.
1399
+
The [IWebHostEnvironment.WebRootPath](xref:Microsoft.AspNetCore.Hosting.IWebHostEnvironment.WebRootPath) property determines the relative path to the app's static assets. If the path doesn't exist, a no-op file provider is used.
1366
1400
1367
1401
**Key**: `webroot`
1368
1402
**Type**: `string`
1369
-
**Default**: The default is `wwwroot`. The path to *{content root}/wwwroot* must exist. If the path doesn't exist, a no-op file provider is used.
1403
+
**Default**: The default is `wwwroot`. The path to *{content root}/wwwroot* must exist.
1370
1404
**Environment variable**: `<PREFIX_>WEBROOT`
1371
1405
1372
-
To set this value, use the environment variable or call `UseWebRoot`:
1406
+
To set this value, use the environment variable or call `UseWebRoot` on `IWebHostBuilder`:
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -199,7 +199,7 @@ The content root is the base path for:
199
199
* Data files (*.db*)
200
200
* The [Web root](#web-root), typically the *wwwroot* folder.
201
201
202
-
During development, the content root defaults to the project's root directory. This directory is also the base path for both the app's content files and the [Web root](#web-root). Specify a different content root by setting its path when [building the host](#host). For more information, see [Content root](xref:fundamentals/host/generic-host#contentrootpath-1).
202
+
During development, the content root defaults to the project's root directory. This directory is also the base path for both the app's content files and the [Web root](#web-root). Specify a different content root by setting its path when [building the host](#host). For more information, see [Content root](xref:fundamentals/host/generic-host#contentroot).
203
203
204
204
## Web root
205
205
@@ -209,7 +209,7 @@ The web root is the base path for public, static resource files, such as:
209
209
* JavaScript (*.js*)
210
210
* Images (*.png*, *.jpg*)
211
211
212
-
By default, static files are served only from the web root directory and its sub-directories. The web root path defaults to *{content root}/wwwroot*. Specify a different web root by setting its path when [building the host](#host). For more information, see [Web root](xref:fundamentals/host/generic-host#webroot-1).
212
+
By default, static files are served only from the web root directory and its sub-directories. The web root path defaults to *{content root}/wwwroot*. Specify a different web root by setting its path when [building the host](#host). For more information, see [Web root](xref:fundamentals/host/generic-host#webroot).
213
213
214
214
Prevent publishing files in *wwwroot* with the [\<Content> project item](/visualstudio/msbuild/common-msbuild-project-items#content) in the project file. The following example prevents publishing content in *wwwroot/local* and its sub-directories:
By [Valeriy Novytskyy](https://github.com/01binary) and [Rick Anderson](https://twitter.com/RickAndMSFT)
14
14
15
-
This sample shows you how to enable users to sign in with their Microsoft account using the ASP.NET Core 3.0 project created on the [previous page](xref:security/authentication/social/index).
15
+
This sample shows you how to enable users to sign in with their work, school, or personal Microsoft account using the ASP.NET Core 3.0 project created on the [previous page](xref:security/authentication/social/index).
0 commit comments