Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Host and WebHost class in asp.net core

I was trying to migrate the my application from asp.net core 2.1 to 3.0 and there come a first suggested change in program.cs for creation of host.

asp.net core 2.1 program.cs

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

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

asp.net core 3.0 program.cs

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

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

I can see that in asp.net core 3.0 it is creating Host instead of WebHost and injecting WebHostBuilder while crating Host.

But I don't have an clear idea here what is difference between Host and WebHost and why asp.net core 3.0 application does not allow to create WebHost?

like image 307
Ashish Rathi Avatar asked Jan 15 '20 05:01

Ashish Rathi


People also ask

What is a host in ASP.NET Core?

The host 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. This article covers the Web Host, which remains available only for backward compatibility.

What is difference between generic host and web host in .NET Core?

As name suggests Web host can be used only for HTTP (eg tied for web application) only but generic host, which has been introduced in . Net core 3.0, can be used for Console application as well. Though the Generic host got included in . NET core 2.1 it was only used for non HTTP workloads.

What is the difference between IHostBuilder and IWebHostBuilder?

IHostBuilder : The host builder constructs the host and configures various services. This is the generalization of the previous IWebHostBuilder but also basically does the same just for generic IHost . It configures the host before the application starts. There is the Host.

What does webhost CreateDefaultBuilder () do?

CreateDefaultBuilder() Initializes a new instance of the WebHostBuilder class with pre-configured defaults.


Video Answer


1 Answers

The difference one could see in .NET Core 3.0 vs .NET core 2.2 code is that .NET core 3.0 uses the Generic Host while .NET Core 2.2 use the Web Host for web application. The Generic host got included with ASP.NET CORE 2.1 and became the de-facto standard for the future version of .NET Core. Though the Generic host got included in .NET core 2.1 it was only used for non HTTP workloads. In.NET Core 3.0 it became a universal standard (HTTP + non HTTP workloads).

The reason for shifting from WebHost builder to more generic Host builder is because the WebHost builder was tied more to HTTP request and works well for Web application but with the advent of Microservices and Docker it felt the need of a more generic Web host so .NET Core team revamped it, making it usable with console application also. With Generic Host it is possible to utilize the logging, configuration, and DI libraries within a console application.

To create a Host we can use the new HostBuilder, which has a similar set of methods and extensions as the existing WebHostBuilder.There is one main difference to be aware of and that is 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 the final service provider.

Reason to use ConfigureWebHostDefaults is that the new host builder is a Generic Host Builder so it is important to tell that we intend to configure the default settings for a Web Host.

Please refer to the Microsoft reference which recommends using Generic Host here

like image 178
Shubhanshu Rastogi Avatar answered Oct 09 '22 23:10

Shubhanshu Rastogi