π 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:
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
Built for Web Apps Only: Web Host is tightly coupled with ASP.NET Core MVC or Razor Pages applications.
Uses Kestrel or IIS: It mainly works with Kestrel (a cross-platform web server) or IIS (Windows web server).
Startup.cs Support: The Web Host requires a Startup
class where you configure services and the middleware pipeline.
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
Universal Host: Can be used for Web Apps, Worker Services, Console Apps, Background Services, gRPC apps, and more.
Service-Oriented: Provides built-in support for Dependency Injection (DI), Logging, and Configuration.
IHostedService Support: Allows running background tasks or long-running services.
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
Feature | Web Host | Generic Host |
---|
Purpose | Only for Web Applications | For Web + Non-Web Applications |
Introduced In | ASP.NET Core 1.0 | .NET Core 2.1 |
Flexibility | Limited to Web Apps | Highly Flexible (Web, Console, Worker Services) |
Background Tasks | Not directly supported | Supports IHostedService for background jobs |
Future Usage | Legacy, not recommended for new projects | Recommended 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.