net core 的Generic Host 之Generic Host Builder

前言

通用Host(Generic Host) 与 web Host 不同的地方就是通用Host解耦了Http请求管道,使得通用Host拥有更广的应用场景。比如:消息收发、后台任务以及其他非http的工作负载。这些场景都可以通过使用通用Host拥有横切(Cross-cutting)的能力,比如:配置、依赖注入和日志记录。


Generic Host Builder

Asp net core 2.1版本推出了Generic Host Builder,但它仅仅用在了非http工作负载的场景,Generic Host Builder会在2019年发布的3.0版本中替换掉Web Host Builder。

avatar

2.x中的Generic Host Builder

asp net core 2.1没有使用Generic Host Builder,那么它的使用场景是什么呢?Generic Host Builder的在非http负载的使用场景有消息收发、后台任务等。
HostBuilder位于 Microsoft.Extensions.Hosting 命名空间下,实现了IHostBUilder接口。Net core 应用在Main()中最简单的用法如下:

1
2
3
4
5
6
7
public static async Task Main(string[] args)
{
var host = new HostBuilder()
.Build();

await host.RunAsync();
}

Build()方法是初始化host实例,它仅仅能被调用一次,在Build()方法执行前调用ConfigureServices()方法可以用来配置host。

1
2
3
4
5
6
7
8
9
var host = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.Configure<HostOptions>(option =>
{
// option.SomeProperty = ...
});
})
.Build();

ConfigureServices((hostContext, services) 方法有一个HostBuilderContext参数和一个依赖注入的IServiceCollection参数。你也可以通过调用Configure()设置Host的其他设置,当前HostOptions对象只有一个Shutdown Timeout 属性。
你可以在官方示例看到更多的配置,下面是一个其中的代码片段:

Host 配置部分

1
2
3
4
5
6
7
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
})

应用配置部分

1
2
3
4
5
6
7
8
9
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddJsonFile("appsettings.json", optional: true);
configApp.AddJsonFile(
$"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
optional: true);
configApp.AddEnvironmentVariables(prefix: "PREFIX_");
configApp.AddCommandLine(args);
})

依赖注入代码

1
2
3
4
5
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<LifetimeEventsHostedService>();
services.AddHostedService<TimedHostedService>();
})

日志配置代码

1
2
3
4
5
.ConfigureLogging((hostContext, configLogging) =>
{
configLogging.AddConsole();
configLogging.AddDebug();
})

3.0web应用中的Generic Host Builder

Asp net core 3.0 中使用Generic Host Builder 替换 Web Host Builder,net core 3.0 web 应用在Main函数中简单的使用方式代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void Main(string[] args)
{
CreateHostBuilder(args)
.Build()
.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

3.0版本中的CreateHostBuilder方法与2.x版本的 CreateWebHostBuilder() 方法很相似,二者最大的不同就是WebHost.CreateDefaultBuilder() 被替换成 Host.CreateDefaultBuilder(),
还有一个不同的地方就是 Host.CreateDefaultBuilder()方法,因为新版本的host builder是一个通用的host builder,这样就要通过嗲用 CreateDefaultBuilder()方法来构建一个web app host。

最后

未来我们需要知道:

  • WebHostBuilder在未来将会被弃用
  • IWebHostBuilder接口将会被保留
  • 你不能在Startup类里面注入任何服务,IHostingEnvironment and IConfiguration除外

参考链接

官方文档
Generic Host Builder in ASP .NET Core

文章目录
  1. 1. 前言
  2. 2. Generic Host Builder
  3. 3. 2.x中的Generic Host Builder
  4. 4. 3.0web应用中的Generic Host Builder
  5. 5. 最后
  6. 6. 参考链接
|