Wednesday, September 25, 2024

Difference between WebApplication and WebApplicationBuilder classes in ASP.NET Core

In this post, we will learn the difference between WebApplication and WebApplicationBuilder classes which are available in ASP.NET Core .NET6 and later versions.

In ASP.NET Core .NET 6 and later, WebApplication and WebApplicationBuilder are part of the new minimal hosting model, which simplifies the process of setting up and running web applications. 

The difference between the two is explained point by point in the following sections. Look at their purposes, properties and methods which explain their differences.

1. WebApplication

Purpose:

It represents the actual application. It is responsible for handling HTTP requests and starting the web server.

Role:

After building the application, it manages the middleware pipeline and listens for incoming HTTP requests.

Key Methods:

  • Create
  • To Initializes a new instance of the web application. It can take string type arguments from command. It returns static WebApplicationBuilder type object.
  • CreateBuilder
  • To Initializes a new instance of the web application. It has all features of Create method. But it can also take parameter of type WebApplicationOptions. This is used to configure the application before building it.
  • CreateSlimBuilder
  • It initializes a new instance of the WebApplicationBuilder class with minimal defaults.
  • Map, MapGet, MapPost etc
  • These methods are used to develop minimal APIs.
  • MapControllers
  • Used to define HTTP request endpoints.
  • UseMiddleware
  • Used to configure the middleware pipeline.
  • Run
  • Starts the application. This method is typically at the end of the configuration process.
var app = builder.Build();
app.MapControllers();
app.Run();

2. WebApplicationBuilder

Purpose:

It is used to configure and create a WebApplication instance.

Role:

It helps you set up your application's configuration, services (dependency injection), logging, and middleware pipeline.

Key Properties:

  • Configuration
  • It returns ConfigurationManager (which returns a collection of configuration providers) which is used to configure based on configuration sources. Mainly used to access the application's configuration settings.
  • Logging
  • It returns an object of ILoggingBuilder which returns a collection of logging providers for the application.
  • Host
  • It returns an object of ConfigureHostBuilder which is used to configure generic host.
  • WebHost
  • It returns an object of ConfigureWebHostBuilder which is used to configure server specific properties.
  • Environment
  • It returns an object of IWebHostEnvironment which provides information about web hosting environment.
  • Services
  • It represensts IServiceCollection, it is used to add services to the application's DI container.

Key Method:

  • Build()
  • Once the configuration is done, the Build() method is called to create the WebApplication.

Example:


var builder = WebApplication.CreateBuilder(args); // get WebApplicationBuilder
var configuration = builder.Configuration; // access the application's configuration settings
var env = builder.Environment; // information about web hosting environment
builder.Services.AddControllers();
var app = builder.Build();
app.Run(); // run application by hosting it in the running server

Summary:

WebApplicationBuilder is responsible for configuring and preparing ASP.NET Core application. WebApplication represents the actual running application that handles requests. In essence, the WebApplicationBuilder can be used to set everything up(logging, configuration provider, web hosting environment etc.), and then create the WebApplication which actually runs the server and processes HTTP requests.

No comments:

Post a Comment

Hot Topics