我在通用主机构建器类( HostBuilder
)上看到 2 个几乎相同的扩展方法: ConfigureWebHostDefaults
和 ConfigureWebHost
.它们具有相同的签名并位于不同的程序集中。我看到了 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
配置一个网络主机:另外,官方文件提到:
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/