DEV Community

Cover image for Exploring the Program.cs File: Key Components and Configuration in ASP.NET Web API
Emre Kocadere
Emre Kocadere

Posted on

Exploring the Program.cs File: Key Components and Configuration in ASP.NET Web API

The Program.cs is the main class for .NET applications. It starts the application and allows us to configure the application.
It allows us to add things like dependencies, configuration files, and middlewares.

This is a default Program.cs file for ASP.NET.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();
app.MapControllers();

app.Run();
Enter fullscreen mode Exit fullscreen mode

Key Components.

1-WebApplication:creates the WebApplicationBuilder instance necessary for your application and performs the basic configurations.

2-WebApplicationBuilder:It is used to start ASP.NET Core applications and to configure the application's settings and services.
You can add dependencies and services using the Services property of the WebApplicationBuilder class.It is used to add services to the dependency injection container. These services provide the objects needed throughout the application.
For Example

builder.Services.AddDbContext<UserManagementContext>(options => options.UseNpgsql(connectionString));

builder.Services.AddScoped<GuidanceService>();
Enter fullscreen mode Exit fullscreen mode

In these lines,
builder.Services.AddDbContext(options => options.UseNpgsql(connectionString)); registers the UserManagementContext with the dependency injection container using Npgsql for PostgreSQL.
builder.Services.AddScoped(); This line registers the GuidanceService as scoped in the dependency injection container, meaning a new instance is created for each request.

builder.Services represents an instance of IServiceCollection,
IServiceCollection is used to register dependencies (services) in your application.
Its official description is as follows:
"A collection of services for the application to compose. This is useful for adding user-provided or framework-provided services."

Also, you can add and read configuration files using the WebApplicationBuilder.

builder.Configuration.AddJsonFile("ConnectionString.json");

var connectionString = builder.Configuration["ConnectionString"];

Enter fullscreen mode Exit fullscreen mode

This code snippet adds the ConnectionString.json file to the configuration and then retrieves the value associated with the "ConnectionString" key from the configuration.

3-var app = builder.Build();:This line creates a WebApplication instance using all the services configured above. This is a fundamental step for building and starting the application.

Additionally, you can add middleware using the app object, which is an instance of the WebApplication. For example, middleware like app.UseHttpsRedirection();, app.UseAuthorization();, and app.MapControllers(); has been added here.

The application's startup process begins at the builder.Build() line. The application's execution starts at the app.Run() line.

Top comments (0)