.NET Core  

Difference Between Generic Host and Web Host in .NET Core

πŸ“– Introduction

When you start working with .NET Core applications, one of the first things you’ll come across is the concept of hosts. A host is responsible for starting your app, managing its lifetime, handling configurations, and providing dependency injection services. In .NET Core, there are mainly two types of hosts:

  • Generic Host

  • Web Host

At first glance, they may seem similar, but they serve different purposes. In this article, we’ll break down the difference between Generic Host and Web Host in .NET Core, explain their roles in simple terms, and help you understand when to use each.

πŸš€ What is a Web Host?

The Web Host was introduced with the early versions of ASP.NET Core. It is specifically designed for web applications.

Key Points

  1. Built for Web Apps Only: Web Host is tightly coupled with ASP.NET Core MVC or Razor Pages applications.

  2. Uses Kestrel or IIS: It mainly works with Kestrel (a cross-platform web server) or IIS (Windows web server).

  3. Startup.cs Support: The Web Host requires a Startup class where you configure services and the middleware pipeline.

  4. Less Flexible: Since it is web-only, you cannot use it for console apps, worker services, or background jobs.

Example of Web Host

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

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

βœ… This is the classic setup you will find in older ASP.NET Core projects.

βš™οΈ What is a Generic Host?

The Generic Host was introduced in .NET Core 2.1 to make hosting more flexible and unified. Unlike the Web Host, it is not restricted to web apps only.

Key Points

  1. Universal Host: Can be used for Web Apps, Worker Services, Console Apps, Background Services, gRPC apps, and more.

  2. Service-Oriented: Provides built-in support for Dependency Injection (DI), Logging, and Configuration.

  3. IHostedService Support: Allows running background tasks or long-running services.

  4. Simplifies App Hosting: Instead of having different hosts for different workloads, the Generic Host unifies everything under one system.

Example of Generic Host

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

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

βœ… Notice that the Generic Host can also configure web apps using ConfigureWebHostDefaults, but it is not limited to that.

πŸ”‘ Major Differences Between Generic Host and Web Host

FeatureWeb HostGeneric Host
PurposeOnly for Web ApplicationsFor Web + Non-Web Applications
Introduced InASP.NET Core 1.0.NET Core 2.1
FlexibilityLimited to Web AppsHighly Flexible (Web, Console, Worker Services)
Background TasksNot directly supportedSupports IHostedService for background jobs
Future UsageLegacy, not recommended for new projectsRecommended and modern standard

πŸ“Œ Why Microsoft Moved to Generic Host

Microsoft wanted to simplify the development experience by creating one unified hosting model. Instead of having multiple hosts for different types of apps, the Generic Host works everywhere. This makes it easier to:

  • Share common code across different types of apps.

  • Run background jobs and web requests together.

  • Provide a consistent developer experience across all .NET Core projects.

🎯 When Should You Use Which?

  • Use Web Host – Only if you are maintaining an older ASP.NET Core project.

  • Use Generic Host – For all new projects, whether it’s a web app, worker service, console app, or API.

πŸ‘‰ In short, Generic Host is the future, while Web Host is the past.

βœ… Conclusion

Both Web Host and Generic Host are important concepts in .NET Core Hosting. However, as of today, the Generic Host is the recommended approach because it provides flexibility, scalability, and supports a wide range of application types. If you’re starting a new project, always go with the Generic Host.

By understanding this difference, you can make better architectural decisions for your applications and ensure they are ready for the future of .NET.