Tuesday, February 13, 2024

How ASP.NET Core applications boot up and start

 Take the following Program.cs class as example -

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();


Here is an explanation of how ASP.NET Core applications boot up and start:


The entry point is the Main method in Program.cs. This is where the application startup process begins.


The WebApplication.CreateBuilder() method is called to create a new WebApplicationBuilder. This sets up the initial configuration for building the web app host.


Services like MVC controllers are added to the builder to be included in the application. These extend the functionality of the app.


The builder configures the services and components, and then builds the WebApplication object. This is the core app host.


The application configuration methods are called on the built WebApplication. This configures the HTTP request handling pipeline.


Middleware components are added to the pipeline in a specific order. These handle requests and responses.


Endpoints like route mappings and authorization are configured.


Finally app.Run() is called to start the web host, begin listening for requests, and start the app!


ASP.NET Core has now fully booted up and is ready to receive and handle HTTP requests.


Requests come in and flow through the middleware pipeline. Each component can handle the request or pass it to the next one.


Endpoints like MVC controllers take over and execute app logic to generate responses.


Responses flow back out through the middleware pipeline and are sent back to clients.


So in summary, the Main method bootstraps the host, sets up configuration, wires up the pipeline, and launches the app to start handling requests using the configured middleware and endpoints.





No comments:

Post a Comment

What is DaemonSet in Kubernetes

 A DaemonSet is a type of controller object that ensures that a specific pod runs on each node in the cluster. DaemonSets are useful for dep...