前言

通用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的更多相关文章

  1. Asp.net Core 2.1新功能Generic Host(通用主机),了解一下

    什么是Generic Host ? 这是在Asp.Net Core 2.1加入了一种新的Host,现在2.1版本的Asp.Net Core中,有了两种可用的Host. Web Host –适用于托管W ...

  2. net Core 2.1新功能Generic Host(通用主机)

    net Core 2.1新功能Generic Host(通用主机) http://doc.okbase.net/CoderAyu/archive/301859.html 什么是Generic Host ...

  3. 翻译 - ASP.NET Core 基本知识 - 通用主机 (Generic Host)

    翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-5.0 ...

  4. 翻译 - ASP.NET Core 基本知识 - Web 主机 (Web Host)

    翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-5.0 ASP. ...

  5. 4. [mmc subsystem] mmc core(第四章)——host模块说明

    零.说明 对应代码drivers/mmc/core/host.c,drivers/mmc/core/host.h. 为底层host controller driver实现mmc host的申请以及注册 ...

  6. 报错: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 ...

  7. kafka配置项host.name advertised.host.name

    遇到的问题: 在本机或者其他机器telnet IP 9092,通,使用域名也通,telnet 127.0.0.1 9092不通 host.name:按配置文件说明,是Kafka绑定的interface ...

  8. nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比

    转载自https://www.jianshu.com/p/7a8a7eb3707a 1.浏览器直接访问服务,获取到的 Host 包含浏览器请求的 IP 和端口 测试服务器,centos 7 sudo ...

  9. linux设置允许和禁止访问的IP host.allow 和 host.deny

    对于能过xinetd程序启动的网络服务,比如ftp telnet,我们就可以修改/etc/hosts.allow和/etc/hosts.deny的配制,来许可或者拒绝哪些IP.主机.用户可以访问. 比 ...

随机推荐

  1. 资料:MVC框架+SQL Server 数据集成引擎

    ylbtech-资料:MVC框架+SQL Server 数据集成引擎 1.返回顶部 1. 功能特点: MVC框架耦合性低视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样 ...

  2. RESTEasy常用注解

    一.@Path,标注资源类或方法的相对路径          Path参数的形式有三种:          1.固定值          2.纯正则表达式          3.固定值和正则表达式的混 ...

  3. SQL Server BCP 资料导入导出

    SQL Server BCP 导入导出使用 Bcp 导出导入数据高效,比使用SQL Server Management Stdio 提供的数据库导出导入要高效因为sql server 也没有提供提供类 ...

  4. Ajax前端调后台方法

    后台对当前页面类进行注册 Ajax.Utility.RegisterTypeForAjax(typeof(Login));//Login 当前类名 在方法上面加 [Ajax.AjaxMethod(Aj ...

  5. 菜鸟级的Git与GitHub使用总结(转)

    菜鸟级的Git与GitHub使用总结 原创 2016年12月01日 14:58:30 1792 前言 这几天一直在折腾学习Git和GitHub的使用.几天下来,在网上查阅了大量的资料,总算有一些成果. ...

  6. 各版本Google浏览器下载地址

    各版本谷歌浏览器下载地址 https://www.chromedownloads.net/chrome64win/

  7. Ensembl突变数据描述之(一)——突变物种数据库及预测工具

    以下是对Ensembl突变数据库中储存的数据的描述,对于Ensembl数据库中不同的物种,我们从各种来源(例如,dbSNP数据库)导入突变数据(SNP.CNV.等位基因频率.基因型等),导入的突变数据 ...

  8. TCP三次握手四次挥手原理

    转自http://www.cnblogs.com/liuxiaoming/archive/2013/04/27/3047803.html TCP协议三次握手原理: 首先,给张图片,建立TCP三次握手的 ...

  9. 如何使用visual studio 2017创建C语言项目

    使用visual studio 2017创建一个C语言项目,步骤如下: (1)打开Visual Studio 2017环境后出现欢迎界面,如图1所示. 图1  Visual Studio 2017欢迎 ...

  10. CodeForces 125D【鸽巢原理】

    哇塞?开始的三个数其中两个数一定能确定一个序列.(鸽巢原理) #include <bits/stdc++.h> using namespace std; typedef long long ...