Sobes.tech
Junior — Middle

What is the main role of the ConfigureServices method in application setup?

sobes.tech AI

Answer from AI

The ConfigureServices method in a C# application (for example, in ASP.NET Core) is responsible for registering and configuring services that will be used through the dependency injection mechanism.

In this method, you define which services and with what lifetime (Singleton, Scoped, Transient) will be available in the application. This allows centralized management of dependencies and facilitates testing and maintenance.

Example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddDbContext<MyDbContext>(options => ...);
    services.AddScoped<IMyService, MyService>();
}

Here, controllers, database context, and custom services are registered.