In ASP.NET Core, the WebApplicationBuilder class provides both the Host and WebHost properties, and each serves a different purpose when configuring the application. Understanding when to use Host and when to use WebHost depends on whether you need to configure generic host settings or web-specific settings. Here's an explanation of each property and guidance on when to use them:
Host Property
- Type: ConfigureHostBuilder
- Purpose: The Host property is used to configure generic hosting settings for the application. This applies to aspects of the host that are not specific to web hosting but are common across all kinds of .NET applications, such as logging, dependency injection, and other host-level services.
- When to use Host:
- When you need to configure settings that apply to both web applications and non-web applications.
- When configuring the host-wide services (e.g., logging, configuration sources, generic services).
- When you need to configure the application for background services or worker services, or even console apps, using the same host infrastructure as a web app.
- When you are customizing the dependency injection (DI) system at the host level or configuring logging providers. Examples of scenarios where Host is used:
- Configuring logging providers:
builder.Host.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
});
- Adding a configuration source:
builder.Host.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("customsettings.json", optional: true);
});
- Setting up background services or hosted services:
builder.Services.AddHostedService<MyBackgroundService>();
WebHost Property
- Type: ConfigureWebHostBuilder
- Purpose: The WebHost property is used to configure web server-specific settings that are unique to hosting web applications. This is where you customize settings related to the web server, such as Kestrel, IIS, request pipeline, URLs, and other web-specific configurations.
- When to use WebHost:
- When you need to configure the web server (e.g., Kestrel, IIS, HTTP.sys).
- When you need to set the URLs on which the web server listens.
- When you need to configure middleware that operates at the server level.
- When you need to set up HTTPS certificates or other web server settings.
- When you need to modify the request-handling pipeline or customize the server's behavior (e.g., request limits, timeouts). Examples of scenarios where WebHost is used:
- Configuring Kestrel to listen on specific ports:
builder.WebHost.UseKestrel(options =>
{
options.ListenLocalhost(5000);
});
- Setting up the web server URLs:
builder.WebHost.UseUrls("http://localhost:5000");
- Configuring IIS integration:
builder.WebHost.UseIISIntegration();
- Customizing the hosting environment or handling custom server settings:
builder.WebHost.UseSetting("https_port", "443");
Key Differences between Host and WebHost
When to Use WebHost:
- When configuring the web server settings such as Kestrel or IIS.
- When dealing with URLs, certificates, ports, or other settings related to web hosting.
- For configuring how HTTP requests are processed by the server.
Here’s an example where both Host and WebHost are configured:
var builder = WebApplication.CreateBuilder(args);
// 1. Configure host-wide settings (applies to all types of apps, not just web apps)
builder.Host.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
});
// 2. Configure web server-specific settings (applies only to web apps)
builder.WebHost.UseKestrel(options =>
{
options.ListenAnyIP(5000); // Web server listening on port 5000
});
// Add services to the container
builder.Services.AddControllers();
// Build and run the application
var app = builder.Build();
app.UseRouting();
app.MapControllers();
app.Run();
Conclusion
- Use Host to configure settings that apply across different types of applications (including background services, logging, and configuration).
- Use WebHost when configuring settings specifically related to web servers, such as Kestrel, IIS, HTTPS, and other web-specific configurations. If you're working on a web application with server-specific customizations, focus on the WebHost. If you're configuring broader host-level settings (like logging or background services), use the Host.
No comments:
Post a Comment