In ASP.NET Core .NET 6 and later, the process of creating a server has been simplified with the introduction of the Minimal Hosting Model, which reduces boilerplate code and streamlines the process. The server creation process revolves around configuring and launching an instance of WebApplication, which hosts the application and handles HTTP requests.
Steps in Server Creation
Step1. Minimal Hosting Model and CreateBuilder
In ASP.NET Core .NET 6 and later versions, the server is created when you call the WebApplication.CreateBuilder() method, which configures a Kestrel server by default (or IIS when hosted in an IIS environment). The WebApplicationBuilder sets up the basic infrastructure for the app, including the server setup, middleware, dependency injection (DI), and other services.var builder = WebApplication.CreateBuilder(args); // Initializes the builder
Step2. Configure Services
Using the builder.Services property, you can configure any services your app needs (e.g., controllers, logging, database contexts). This is where the dependency injection container is set up.
Example:
builder.Services.AddControllers(); // Add MVC controllers to the app
Step3. Configure Middleware Pipeline
After building the application, you configure the middleware pipeline using the returned WebApplication object. Middleware components, such as routing, authentication, or custom middleware, are added in the pipeline.
Example:
var app = builder.Build(); // Builds the application (and server)
app.UseRouting(); // Configure middleware for routing
app.MapControllers(); // Map controller routes
Step4. Server Startup
The server is actually created and started when you call the app.Run() method. This starts the Kestrel server (or IIS if you're hosting in IIS) and begins listening for incoming HTTP requests. Example:
app.Run(); // Starts the Kestrel server and runs the app
Default Server: Kestrel
In ASP.NET Core .NET 6 and later versions, the Kestrel web server is the default server used to host the application unless you explicitly configure another server (e.g., IIS). Kestrel is a cross-platform, high-performance, HTTP server.
Example of Full Server Creation in .NET 6 and later versions.
Here’s a complete example of how a server is created using the minimal hosting model in ASP.NET Core .NET 6 and later versions.:
var builder = WebApplication.CreateBuilder(args); // 1. Create a builder
builder.Services.AddControllers(); // 2. Register services (dependency injection)
var app = builder.Build(); // 3. Build the WebApplication
app.UseRouting(); // 4. Configure middleware pipeline
app.MapControllers();
app.Run(); // 5. Start the Kestrel server
Detailed Breakdown of code
CreateBuilder(args): This sets up the host for the application, which includes configuration, logging, and DI. It also configures Kestrel as the web server by default.
builder.Build():
After configuring services and middleware, the Build() method creates a WebApplication instance that is ready to run. Internally, it also configures the server (Kestrel).
app.Run():
When Run() is called, the server (Kestrel) is started and begins to listen for HTTP requests on the configured port (typically port 5000 for HTTP and 5001 for HTTPS). This is the entry point for starting the web server and processing requests.
Server Customization
If you need to customize the server, you can configure Kestrel or use another server like IIS or HTTP.sys:
Customizing Kestrel
You can configure Kestrel server settings using the ConfigureWebHostDefaults method:
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel(options =>
{
options.ListenLocalhost(5000); // Customize Kestrel to listen on specific ports
});
var app = builder.Build();
app.Run();
Using IIS or IIS Express
ASP.NET Core apps can run behind IIS (or IIS Express during development), with IIS acting as a reverse proxy to the Kestrel server. To enable IIS, you'd typically use the following configuration:
builder.WebHost.UseIISIntegration(); // For IIS Integration
Summary
- In ASP.NET Core .NET 6 and later versions., the Kestrel server is created automatically when you use WebApplication.CreateBuilder() and start the app with app.Run().
- The Minimal Hosting Model simplifies the setup process by providing default configurations, including the use of Kestrel as the web server. You can customize the server (e.g., Kestrel, IIS) by configuring options via builder.WebHost.
- Thus, the server creation process in ASP.NET Core .NET 6 and later versions, is both streamlined and flexible, giving developers a powerful yet simple setup by default.
No comments:
Post a Comment