Skip to content

Commit 9d3d803

Browse files
Generic Host clean up (#17783)
* Generic Host clean up * Generic Host clean up * Generic Host clean up
1 parent 67231ba commit 9d3d803

1 file changed

Lines changed: 44 additions & 10 deletions

File tree

aspnetcore/fundamentals/host/generic-host.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ uid: fundamentals/host/generic-host
1212

1313
::: moniker range=">= aspnetcore-3.0 <= aspnetcore-3.1"
1414

15-
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>).
1616

17-
## What's a host?
17+
## Host definition
1818

1919
A *host* is an object that encapsulates an app's resources, such as:
2020

@@ -27,16 +27,33 @@ When a host starts, it calls `IHostedService.StartAsync` on each implementation
2727

2828
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.
2929

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-
3230
## Set up a host
3331

3432
The host is typically configured, built, and run by code in the `Program` class. The `Main` method:
3533

3634
* Calls a `CreateHostBuilder` method to create and configure a builder object.
3735
* Calls `Build` and `Run` methods on the builder object.
3836

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:
38+
39+
```csharp
40+
public class Program
41+
{
42+
public static void Main(string[] args)
43+
{
44+
CreateHostBuilder(args).Build().Run();
45+
}
46+
47+
public static IHostBuilder CreateHostBuilder(string[] args) =>
48+
Host.CreateDefaultBuilder(args)
49+
.ConfigureWebHostDefaults(webBuilder =>
50+
{
51+
webBuilder.UseStartup<Startup>();
52+
});
53+
}
54+
```
55+
56+
The following code creates a non-HTTP workload with a `IHostedService` implementation added to the DI container.
4057

4158
```csharp
4259
public class Program
@@ -970,9 +987,9 @@ public class MyClass
970987

971988
::: moniker range=">= aspnetcore-5.0"
972989

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

975-
## What's a host?
992+
## Host definition
976993

977994
A *host* is an object that encapsulates an app's resources, such as:
978995

@@ -985,16 +1002,33 @@ When a host starts, it calls `IHostedService.StartAsync` on each implementation
9851002

9861003
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.
9871004

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-
9901005
## Set up a host
9911006

9921007
The host is typically configured, built, and run by code in the `Program` class. The `Main` method:
9931008

9941009
* Calls a `CreateHostBuilder` method to create and configure a builder object.
9951010
* Calls `Build` and `Run` methods on the builder object.
9961011

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:
1013+
1014+
```csharp
1015+
public class Program
1016+
{
1017+
public static void Main(string[] args)
1018+
{
1019+
CreateHostBuilder(args).Build().Run();
1020+
}
1021+
1022+
public static IHostBuilder CreateHostBuilder(string[] args) =>
1023+
Host.CreateDefaultBuilder(args)
1024+
.ConfigureWebHostDefaults(webBuilder =>
1025+
{
1026+
webBuilder.UseStartup<Startup>();
1027+
});
1028+
}
1029+
```
1030+
1031+
The following code creates a non-HTTP workload with a `IHostedService` implementation added to the DI container.
9981032

9991033
```csharp
10001034
public class Program

0 commit comments

Comments
 (0)