简体   繁体   中英

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?

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

The host is responsible for application startup and lifetime management. The server is responsible for accepting HTTP requests. Part of the host's responsibility includes ensuring the application's services and the server are available and properly configured. You can think of the host as being a wrapper around the server. The host is configured to use a particular server; the server is unaware of its host.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;

namespace WebApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

You create a host using an instance of WebHostBuilder. This is typically done in your app's entry point: public static void Main. A typical Program.cs, shown below, demonstrates how to use a WebHostBuilder to build a host.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM