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

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

问题描述

在通用主机生成器类( HostBuilder )上,我看到2种几乎相同的扩展方法: ConfigureWebHostDefaults ConfigureWebHost .它们具有相同的签名,并且位于不同的程序集中.我在指南中看到了 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 使用以下命令配置Web主机:

For the difference, ConfigureWebHostDefaults configures a web host with:

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

此外,官方文件提到:

ConfigureWebHostDefaults方法从前缀为"ASPNETCORE_"的环境变量中加载主机配置.将Kestrel服务器设置为Web服务器,并使用应用程序的托管配置提供程序对其进行配置.有关Kestrel服务器的默认选项,请参见ASP.NET Core中的Kestrel Web服务器实现.添加主机过滤中间件.如果ASPNETCORE_FORWARDEDHEADERS_ENABLED = true,则添加转发的标题中间件.启用IIS集成.有关IIS默认选项,请参阅Windows上具有IIS的Host 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/zh-cn/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.0#default-builder-settings

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

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