Last update:

What does WebHost’s CreateDefaultBuilder do?

ASP.NET Core 2 introduced a CreateDefaultBuilder helper method that configures a WebHost with default settings. This post shows what are these defaults.

The CreateDefaultBuilder method does the following:

  • Uses Kestrel server
  • Sets current directory as the Content Root
  • Adds the appsettings.json and appsettings.{EnvironmentName}.json files as a config source
  • Adds user secrets as a config source (only in development environment)
  • Adds environment variables and commandline arguments as config sources
  • Configures logging with settings from the Logging configuration section
  • Adds Console and Debug loggers
  • Uses IIS integration
  • Adds a scope validator that checks if scoped services are not resolved from the root provider (in development only)

If you need to customize this logic, just grab the below code (that’s exactly what gets called by the framework), replace the .CreateDefaultWebHost() method call with it and make any changes you need. Remember you still need to call .UseStartup<>() and .Build().

Interestingly, the commandline configuration provider is added twice. This has been noticed by the team but considered not to be an issue.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Michał Dudak