Wednesday, September 25, 2024

Use of WebApplicationBuilder class in ASP.NET Core

Explain different properties of WebApplicationBuilder class?

The WebApplicationBuilder class in ASP.NET Core plays a crucial role in configuring and setting up your web application. It simplifies the configuration of the application’s services, middleware, and other essential settings in the minimal hosting model introduced in .NET 6+.

The WebApplicationBuilder has a number of useful properties. Each property has a return type. For example, builder.Configuration returns an object of type IConfiguration where builder is object of WebApplicationBuilder type. Look at the code below.

IConfiguration configuration= builder.Configuration;

The key properties of the WebApplicationBuilder class are given below:

  1. Configuration
  • Type: IConfiguration
  • Description: This property provides access to the application’s configuration settings, such as those from appsettings.json, environment variables, command-line arguments, and other configuration providers.
  • Usage: You can use this to retrieve configuration values.
var mySetting = builder.Configuration["MySetting"];
  1. Environment
  • Type: IWebHostEnvironment
  • Description: This provides information about the current hosting environment, such as the EnvironmentName (e.g., Development, Production, etc.), ContentRootPath, and WebRootPath. This is useful for environment-specific behavior or configurations.
  • Usage: Access the current environment or paths.
if (builder.Environment.IsDevelopment())
{
    // Development specific configuration
}
  1. Host
  • Type: ConfigureHostBuilder
  • Description: This property is used to configure the host (the environment in which the app runs), such as managing the underlying server, setting up logging, and configuring the host's behavior (like Kestrel, HTTP.sys, etc.). You can add advanced host-level settings here.
  • Usage: Customize the host.
builder.Host.ConfigureLogging(logging =>
{
    logging.AddConsole();
});
  1. Logging
  • Type: ILoggingBuilder
  • Description: This allows you to configure logging providers for the application. You can add custom logging frameworks like Serilog, or configure built-in providers like Console, Debug, etc.
  • Usage: Add logging configuration.
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
  1. Services
  • Type: IServiceCollection
  • Description: This property is a collection where you register the application’s dependencies for dependency injection (DI). It’s used to add services like controllers, database contexts, custom services, and other dependencies.
  • Usage: Add services to DI container.
builder.Services.AddControllers();
builder.Services.AddDbContext<MyDbContext>();
  1. WebHost
  • Type: ConfigureWebHostBuilder
  • Description: This is used to configure web-specific settings for the application, such as setting up the web server (Kestrel, IIS), configuring the startup behavior, or adding middleware. It provides customization options for the ASP.NET Core web server and pipelines.
  • Usage: Customize web host behavior.
builder.WebHost.UseKestrel()
              .UseUrls("http://localhost:5000");
  1. Properties
  • Type: IDictionary<object, object>
  • Description: This is a key-value store where you can store custom state or information that might be needed during the application’s lifetime. It’s useful for passing data between components of the app during setup.
  • Usage: Add custom properties.
builder.Properties["MyCustomKey"] = "MyCustomValue";
  1. ConfigurationSources
  • Type: IList
  • Description: This collection holds the list of configuration sources used by the application. You can use this property if you need to examine or modify how configuration sources (such as appsettings.json, environment variables, or command-line arguments) are handled.
  • Usage: Add or inspect configuration sources.
builder.ConfigurationSources.Add(new MyCustomConfigSource());
  1. HostEnvironment
  • Type: IHostEnvironment
  • Description: This property is similar to Environment and provides access to the hosting environment (like Development, Production, or Staging). This is the interface for the environment in which the application is running.
  • Usage: Access the hosting environment.
if (builder.HostEnvironment.IsProduction())
{
    // Production-specific behavior
}
  1. WebApplicationOptions
  • Type: WebApplicationOptions
  • Description: This is a configuration object that can be passed to the CreateBuilder method to customize the application’s creation options, such as setting the content root, web root, application name, or specifying the environment.
  • Usage: Configure app creation settings.
var options = new WebApplicationOptions
{
    ApplicationName = "MyApp",
    ContentRootPath = "/custom/root"
};
var builder = WebApplication.CreateBuilder(options);

These properties are useful in the setup of the ASP.NET Core application at granular level.

No comments:

Post a Comment

Hot Topics