What is 'container' in the file Program.cs

Arshad Nazeer 21 Reputation points
2022-03-07T10:40:28.037+00:00
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using solutions.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/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.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.Run();

At line 7, what is "container" that the author is talking about?

Thanks.

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Accepted answer
  1. Anonymous
    2022-03-08T03:10:59.023+00:00

    Hi @Arshad Nazeer ,

    What is 'container' in the file Program.cs

    This is the Asp.net core built-in IoC container. The built-in IoC container also known as Dependency Injection (DI) Container, it is a programming framework that provides you with an automatic Dependency Injection of your components.

    The DI Container is responsible for supporting automatic Dependency Injection. Its basic features include:

    • Registration: the DI Container needs to know which type of object to create for a specific dependency; so, it provides a way to map a type to a class so that it can create the correct dependency instance.
    • Resolution: this feature allows the DI Container to resolve a dependency by creating an object and injecting it into the requesting class. Thanks to this feature, you don't have to instantiate objects manually to manage dependencies.
    • Disposition: the DI Container manages the lifetime of the dependencies following specific criteria.

    More detail about using the Dependency injection, see Dependency injection in ASP.NET Core


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,851 Reputation points Volunteer Moderator
    2022-03-08T16:02:30.333+00:00

    The line:

    var builder = WebApplication.CreateBuilder(args);

    Creates the DI container accessible via builder.Services

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.