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。

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()中最简单的用法如下:
public static async Task Main(string[] args)
{
var host = new HostBuilder()
.Build();
await host.RunAsync();
}
Build()方法是初始化host实例,它仅仅能被调用一次,在Build()方法执行前调用ConfigureServices()方法可以用来配置host。
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 配置部分
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
})
应用配置部分
.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);
})
依赖注入代码
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<LifetimeEventsHostedService>();
services.AddHostedService<TimedHostedService>();
})
日志配置代码
.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函数中简单的使用方式代码如下:
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

net core 的Generic Host 之Generic Host Builder的更多相关文章
- Asp.net Core 2.1新功能Generic Host(通用主机),了解一下
什么是Generic Host ? 这是在Asp.Net Core 2.1加入了一种新的Host,现在2.1版本的Asp.Net Core中,有了两种可用的Host. Web Host –适用于托管W ...
- net Core 2.1新功能Generic Host(通用主机)
net Core 2.1新功能Generic Host(通用主机) http://doc.okbase.net/CoderAyu/archive/301859.html 什么是Generic Host ...
- 翻译 - ASP.NET Core 基本知识 - 通用主机 (Generic Host)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-5.0 ...
- 翻译 - ASP.NET Core 基本知识 - Web 主机 (Web Host)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-5.0 ASP. ...
- 4. [mmc subsystem] mmc core(第四章)——host模块说明
零.说明 对应代码drivers/mmc/core/host.c,drivers/mmc/core/host.h. 为底层host controller driver实现mmc host的申请以及注册 ...
- 报错:Failed on local exception: Host Details : local host is: "master/192.168.52.26"; dest
报错现象 Failed on local exception: com.google.protobuf.InvalidProtocolBufferException: Protocol message ...
- kafka配置项host.name advertised.host.name
遇到的问题: 在本机或者其他机器telnet IP 9092,通,使用域名也通,telnet 127.0.0.1 9092不通 host.name:按配置文件说明,是Kafka绑定的interface ...
- nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比
转载自https://www.jianshu.com/p/7a8a7eb3707a 1.浏览器直接访问服务,获取到的 Host 包含浏览器请求的 IP 和端口 测试服务器,centos 7 sudo ...
- linux设置允许和禁止访问的IP host.allow 和 host.deny
对于能过xinetd程序启动的网络服务,比如ftp telnet,我们就可以修改/etc/hosts.allow和/etc/hosts.deny的配制,来许可或者拒绝哪些IP.主机.用户可以访问. 比 ...
随机推荐
- 【P2P网贷新手入门】详解借款标的种类及其风险
不同于国外的网贷平台以信用借款标为主,在中国,我们投资网贷平台会看到多样借款标,而投资人往往弄不清自己投资的标属于什么类型的标,特点怎么样,风险如何. 抵 押 标 定义:借款人用自己的房屋车辆等实物在 ...
- day18-事务与连接池 4.事务特性
- layui 常用方法 readme
layui样式加载: layui.use(['table', 'element', 'laydate', 'layer'], function () { var table = layui.table ...
- R语言系列:数据的基本运算
基本运算符号 1.基本数学计算 +.-.*./.^.%%(求模).%/%(整除) 注意:求模运算两边若为小数,则整数和小数部分分别求模.例:5.6%%2.2 2.比较运算 >.< ...
- unreal3对象管理模块分析二
上一篇主要记了UClass的创建,现在总结一下UObject的创建,可以从几个不同角度来理解. 从途径上看,可以根据UObject的构造函数来分个类: // Constructors. UObject ...
- 20. Linux提权:从入门到放弃
几点前提 已经拿到低权shell 被入侵的机器上面有nc,python,perl等linux非常常见的工具 有权限上传文件和下载文件 内核漏洞提权 提到脏牛,运维流下两行眼泪,我们留下两行鼻血.内核漏 ...
- 1. xss原理分析与剖析(1)
0×01 前言: <xss攻击手法>一开始在互联网上资料并不多(都是现成的代码,没有从基础的开始),直到刺的<白帽子讲WEB安全>和cn4rry的<XSS跨站脚本攻击剖析 ...
- UVa 11468 Substring (AC自动机+概率DP)
题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...
- Cygwin install apt-cyg
1. UPDATE CYGWIN First of all you will need to ensure that Cygwin has the necessary binaries require ...
- SQL中的union,except,intersect用法
限制:所有查询中的列数和列的数序必须相同 union all:完全整合两个结果集查出所有数据 union:查出两个表的数据并且去除重复的数据 except:去重之后只会保留第一个表中的数据,查询a表在 ...