Rustam
Rustam

Reputation: 123

What is the difference between ConfigureWebHostDefaults and ConfigureWebHost methods?

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?

Upvotes: 11

Views: 16579

Answers (1)

Anduin Xue
Anduin Xue

Reputation: 3737

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);
            });
        }

It just calls the ConfigureWebHost, but will an additional step: 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

For the difference, ConfigureWebHostDefaults configures a web host with:

  • Use Kestrel as the web server and configure it using the application's configuration providers
  • Adds the HostFiltering middleware,
  • Adds the ForwardedHeaders middleware if ASPNETCORE_FORWARDEDHEADERS_ENABLED=true,
  • Enable IIS integration.

Also, the official document mentioned that:

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.

Document link: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.0#default-builder-settings

Upvotes: 21

Related Questions