Junior — Middle
Explain what AddScoped means in the context of Dependency Injection in ASP.NET Core.
sobes.tech AI
Answer from AI
In ASP.NET Core, the AddScoped concept in Dependency Injection means that the service is created once per HTTP request and reused throughout that request.
That is, if you have a service registered as Scoped, one instance of this service will be created during the processing of a single request, and this instance will be used by all components requesting this service within that request.
This is convenient for services that need to maintain state or data only for the duration of a single request, such as working with a database through a context.
Example of registering a service:
services.AddScoped<IMyService, MyService>();
Thus, each HTTP request will get its own instance of MyService, which will be destroyed after the request is completed.