DotnetCore


Host definition

host is an object that encapsulates an app’s resources, such as:

  • Dependency injection (DI)
  • Logging
  • Configuration
  • IHostedService implementations

CreateDefaultBuilder()

which is a Host Builder with the additional ability of loading default configuration.

CreateDefaultBuilder a generic Host builder in .NET and ASP.NET Core plays an important role in initializing the Host and its configuration like getting access to applications host details including app configuration, logger configuration, user secretes, getting access to environmental configuration etc.

It Initializes a new instance of Microsoft.Extensions.Hosting.HostBuilder class with pre-configured defaults.

Host Builder using CreateDefaultBuilder works in ASP.NET Core and non-host apps like a Console application or WPF application as well.

Typical implementation of CreateWebHostBuilder looks as below,

ASP.NET Core

123public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>();

Non-host Apps

1var builder = new HostBuilder()

Or

1var builder = Host.CreateDefaultBuilder().Build();

Let see in detail how CreateDefaultBuilder helps us managing all the above details.

 summary of the default configuration from the Default Builder:

https://wakeupandcode.com/generic-host-builder-in-asp-net-core-3-1/

ConfigureWebHostDefaults

The following defaults are applied to the Microsoft.AspNetCore.Hosting.IWebHostBuilder:
// 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, and enable IIS integration.