.net-core - ConfigureWebHostDefaults 和 ConfigureWebHost 方法之间有什么区别?

标签 .net-core

我在通用主机构建器类( HostBuilder )上看到 2 个几乎相同的扩展方法: ConfigureWebHostDefaultsConfigureWebHost .它们具有相同的签名并位于不同的程序集中。我看到了 ConfigureWebHostDefaults在指南中,但几乎没有关于 ConfigureWebHost 的内容.它们之间有什么区别?

最佳答案

通过 ASP.NET Core 源代码,ConfigureWebHostDefaults等于:

        public static IHostBuilder ConfigureWebHostDefaults(this IHostBuilder builder, Action<IWebHostBuilder> configure)
        {
            return builder.ConfigureWebHost(webHostBuilder =>
            {
                WebHost.ConfigureWebDefaults(webHostBuilder);

                configure(webHostBuilder);
            });
        }

它只是调用 ConfigureWebHost ,但会增加一个步骤:ConfigureWebDefaults .

至于ConfigureWebDefaults ,源代码很长,放在这里:

https://github.com/aspnet/AspNetCore/blob/1480b998660d2f77d0605376eefab6a83474ce07/src/DefaultBuilder/src/WebHost.cs#L280

对于差异,ConfigureWebHostDefaults配置一个网络主机:
  • 使用 Kestrel 作为 Web 服务器并使用应用程序的配置提供程序对其进行配置
  • 添加 HostFiltering 中间件,
  • 如果 ASPNETCORE_FORWARDEDHEADERS_ENABLED=true,则添加 ForwardedHeaders 中间件,
  • 启用 IIS 集成。

  • 另外,官方文件提到:

    The ConfigureWebHostDefaults method loads host configuration from environment variables prefixed with "ASPNETCORE_". Sets Kestrel server as the web server and configures it using the app's hosting configuration providers. For the Kestrel server's default options, see Kestrel web server implementation in ASP.NET Core. Adds Host Filtering middleware. Adds Forwarded Headers middleware if ASPNETCORE_FORWARDEDHEADERS_ENABLED=true. Enables IIS integration. For the IIS default options, see Host ASP.NET Core on Windows with IIS.



    文档链接:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.0#default-builder-settings

    关于.net-core - ConfigureWebHostDefaults 和 ConfigureWebHost 方法之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905731/

    相关文章:

    .Net Core HttpClientFactory 配置 HttpClientHandler

    c# - 使用 await 从 linq 语句中选择

    c# - EF 核心 2.1。使用自定义数据库架构进行迁移

    c# - Azure function 2.x ILogger 与.net core ILogger 不兼容?

    .net-core - 跳过 csv 文件中的行

    vue.js - 如何解决无法导入模块 '@nuxt/builder' ?

    c# - 通过代码创建Azure ServiceBus队列

    c# - dotnet core 1.1.1、mvc 1.1.2 中未调用模型绑定(bind)程序提供程序

    c# - ML.NET,缺少 "Score Column"

    asp.net - ASPNET5类库抛出当前运行时目标框架与 '<ProjectName>'不兼容