Sobes.tech
Junior — Middle

Mis on meetodi ConfigureServices peamine roll rakenduse seadistamisel?

sobes.tech AI

Vastus AI-lt

In a C# application (for example, in ASP.NET Core), the ConfigureServices method is responsible for registering and configuring services that will be used via 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 makes testing and maintenance easier.

Example:

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

Here, controllers, the database context, and a custom service are registered.