Theme-Logo
  •   .Net
    •   C Sharp(C#)
    •   Web API
    •   Micro Services
    •   ASP.Net
    • ASP.Net MVC
    • .Net Core
  •   Database
    • SQL Server
    • Oracle
    • PostgreSQL
  •   jQuery
    • jQuery Tutorials
    • jQuery Plugins
    • jQuery UI
    • More on jquery
  •   Tutorials
    • Microservices Tutorials
    • DotNet Core Tutorials
    • PostgreSql Tutorials

ASP.NET Core Web Host

The host or web host in ASP.NET is responsible for app startup and lifetime management. At a minimum, the host configures a server and a request processing pipeline. The host can also set up logging, dependency injection, and configuration

Set up a host

Create a host using an instance of IWebHostBuilder. This is typically performed in the app's entry point, the Main method.

                            public class Program
                            {
                                    public static void Main(string[] args)
                                    {
                                      CreateWebHostBuilder(args).Build().Run();
                                    }

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

The code that calls CreateDefaultBuilder is in a method named CreateWebHostBuilder, which separates it from the code in Main that calls Run on the builder object.

CreateDefaultBuilder Roles

CreateDefaultBuilder has many roles in ASP.Net core:

  • Configures Kestrel server as the web server using the app's hosting configuration providers.
  • Sets the content root to the path returned by Directory.GetCurrentDirectory.
  • Loads host configuration
  • Loads app configuration (eg appsettings.json, appsettings.{Environment}.json, Secret Manager, Environment variables and command line arguments)
  • Configures logging for console and debug output.
  • When running behind IIS with the ASP.NET Core Module, CreateDefaultBuilder enables IIS Integration, which configures the app's base address and port. IIS Integration also configures the app to capture startup errors.
  • Sets ServiceProviderOptions.ValidateScopes to true if the app's environment is Development.
  • The WebHostBuilder also allows us to load configuration from multiple sources into a final configuration representation of Key/Value pairs.

                               public class Program
                                    {
                                        public static void Main(string[] args)
                                        {
                                           var host = new WebHostBuilder()
                                            .UseKestrel()
                                            .UseContentRoot(Directory.GetCurrentDirectory())
                                            .UseIISIntegration()
                                            .UseStartup()
                                            .Build();
    
                                            host.Run();
                                        }
                                }
                            
    So why WebHostBuilder if CreateDefaultBuilder is there?

    There is one main difference to be aware of. The HostBuilder doesn’t provide an extension method that allows you to use a startup class as we can with the WebHostBuilder. This decision was made primarily to avoid the need to create two separate DI containers behind the scenes. With the generic host, a single service collection is configured and then used to build a the final service provider.

    The works extremely well for ASP.NET Core web applications, but there were no similar options in the framework for other types of application, until now!

    Trending Post
    Benefits of using ASP.NET Core over ASP.NET.
    Role of Startup class in .Net Core.
    Interview Questions and Answers
    What is Request delegate?
    What is Host in ASP.NET Core?
    Describe the Generic Host and Web Host?
    Describe the Servers in ASP.NET Core?
    How to read values from Appsettings.json file?
    How to handle errors in ASP.NET Core?
    About us

    DotNet Palace is a community platform created and maintained by The articles are mainly focused on Microsoft stack technologies like C#, ASP.Net, MVC, .Net Core, SQL Server and Oracle, PostgreSQL, SQLite etc. To improve the site's content you can send your valuable suggestions at info.dotnetpalace@gmail.com

    Quick links
  • SOLID Principles
  • Questions
  • OOPS Principles
  • DotNet Palace Tutorials
  • Privacy Policy
  • Terms and Condition