ConfigureWebHostDefaults 和 ConfigureWebHost 方法之间有什么区别? [英] What is the difference between ConfigureWebHostDefaults and ConfigureWebHost methods?

查看:41
本文介绍了ConfigureWebHostDefaults 和 ConfigureWebHost 方法之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I see 2 nearly the same extension methods on generic host builder class (HostBuilder): ConfigureWebHostDefaults and ConfigureWebHost. They have the same signature and locate in different assemblies. I saw ConfigureWebHostDefaults in guides but there is nearly nothing about ConfigureWebHost. What is the difference between them?

推荐答案

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

Via ASP.NET Core source code, ConfigureWebHostDefaults equals to:

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

                configure(webHostBuilder);
            });
        }

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

It just calls the ConfigureWebHost, but will an additional step: ConfigureWebDefaults.

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

As for ConfigureWebDefaults, the source code is pretty long and placed here:

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

对于不同之处,ConfigureWebHostDefaults 配置了一个虚拟主机:

For the difference, ConfigureWebHostDefaults configures a web host with:

  • 使用 Kestrel 作为 Web 服务器并使用应用程序的配置提供程序对其进行配置
  • 添加 HostFiltering 中间件,
  • 如果 ASPNETCORE_FORWARDEDHEADERS_ENABLED=true,则添加 ForwardedHeaders 中间件,
  • 启用 IIS 集成.

另外,官方文档中提到:

Also, the official document mentioned that:

ConfigureWebHostDefaults 方法从以ASPNETCORE_"为前缀的环境变量加载主机配置.将 Kestrel 服务器设置为 Web 服务器并使用应用的托管配置提供程序对其进行配置.有关 Kestrel 服务器的默认选项,请参阅 ASP.NET Core 中的 Kestrel Web 服务器实现.添加主机过滤中间件.如果 ASPNETCORE_FORWARDEDHEADERS_ENABLED=true,则添加转发标头中间件.启用 IIS 集成.有关 IIS 默认选项,请参阅使用 IIS 在 Windows 上托管 ASP.NET Core.

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

这篇关于ConfigureWebHostDefaults 和 ConfigureWebHost 方法之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆