.Net Core Host 之详解
阅读原文时间:2023年07月08日阅读:1

简介:

开发使用有三年经验了,想趁这个机会把net core的知识点梳理一下,也更好的研究一下.NET 5给我们带来的变化。

主机的概念:

一个主机是封装了应用程序的资源,比如一个对象:

  • 依赖注入 (DI)
  • 日志记录
  • 配置
  • IHostedService 实现

将应用程序的所有相互依赖的资源包含在一个对象中的主要原因是生命周期管理:控制应用程序启动和正常关闭。

主机的创建

主机通常由Program类中的代码配置、构建和运行。该Main方法:

  • 调用CreateHostBuilder方法来创建和配置构建器对象。
  • 构建器对象上的调用Build和Run方法.

1. Host.CreateDefaultBuilder(args) 主要做了以下几件事:

  • 内容根设置为GetCurrentDirectory返回的路径。
  • 从以下位置加载主机配置:
    • DOTNET_.为前缀的环境变量。
    • 命令行参数。
  • 从以下位置加载应用程序配置:
    • appsettings.json
    • appsettings.{Environment}.json
    • 应用程序在Development环境中运行时的用户机密
    • 环境变量。
    • 命令行参数。
  • 添加以下日志记录提供程序:
    • 安慰
    • 调试
    • 事件源
    • EventLog(仅当在 Windows 上运行时)
  • 当环境为 Development 时启用范围验证依赖项验证

2. 对于http 工作负载,使用ConfigureWebHostDefaults 去实例化一个IWebHostBuilder并且向webbuilder增加功能配置。

public static IHostBuilder ConfigureWebHostDefaults(this IHostBuilder builder,

Action configure)
{
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

        return builder.ConfigureWebHost(webHostBuilder =>  
        {  
            WebHost.ConfigureWebDefaults(webHostBuilder);

            configure(webHostBuilder);  
        });  

}

可以看到,ConfigureWebHostDefaults 是一个扩展方法,在内部调用 ConfigureWebHost 这个扩展方法,

ConfigureWebHost 这个扩展方法又做了哪些事情? 代码链接:https://github.com/dotnet/aspnetcore/blob/main/src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs

public static class GenericHostWebHostBuilderExtensions
{
///

/// Adds and configures an ASP.NET Core web application. ///
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action configure)
{
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

        return builder.ConfigureWebHost(configure, \_ => { });  
    }

    /// <summary>  
    /// Adds and configures an ASP.NET Core web application.  
    /// </summary>  
    public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure, Action<WebHostBuilderOptions> configureWebHostBuilder)  
    {  
        if (configure is null)  
        {  
            throw new ArgumentNullException(nameof(configure));  
        }

        if (configureWebHostBuilder is null)  
        {  
            throw new ArgumentNullException(nameof(configureWebHostBuilder));  
        }

        var webHostBuilderOptions = new WebHostBuilderOptions();  
        configureWebHostBuilder(webHostBuilderOptions);  
        var webhostBuilder = new GenericWebHostBuilder(builder, webHostBuilderOptions);  
        configure(webhostBuilder);  
        builder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());  
        return builder;  
    }  
}

通过实例化一个 GenericWebHostBuilder 实例,扩展HostBuilder ASP.NET Core的运行时配置。

ConfigureWebHost 中的confiure 其实执行的是:

WebHost.ConfigureWebDefaults(webHostBuilder);

configure(webHostBuilder); (2)

(2) 执行的是我们在program 代码里进行的设置。例如 webBuilder.UseStartup()。

有读者比较感兴趣 GenericWebHostBuilder 这个类具体都做了什么,因为内容较多,不放在这里展示,实例化后的 _WebHostBuilder 内部拥有 HostBuilder,并且为其扩展了各种web 运行时的配置;
_

最后,回到 WebHost.ConfigureWebDefaults(webHostBuilder) ,它为注入的 webHostBuilder启用各类配置:

  • 从前缀为 的环境变量加载主机配置ASPNETCORE_
  • Kestrel服务器设置为 Web 服务器并使用应用的托管配置提供程序对其进行配置。
  • 添加主机过滤中间件
  • 如果等于,则添加转发头中间件ASPNETCORE_FORWARDEDHEADERS_ENABLED``true
  • 启用 IIS 集成。

3. 主机的build

通过 CreateHostBuilder(args).Build() 去构建主机。

这其中主要是:

  • 构建配置系统
  • 构建主机环境信息
  • 构建主机build 上下文
  • 构建应用程序配置
  • 创建依赖注入提供程序,依赖注入容器

总而言之:(如果你忽略上面的一整段,此处要看)

(1) 创建了HostBuilder -> (2) 为hostbuilder添加 aspnet core运行时的配置 -> (3) 启用各类web 运行时需要的配置 -> (4) 根据加载好的各种配置构建主机运行时需要提供的服务(configuration, DI Container, 应用程序配置等)

会持续整理发布关于后端和NET Core, .NET 的相关学习和认知,欢迎大家一起讨论学习。