Search code examples
asp.net-coredependency-injection.net-core

Are services added with AddScoped stored internally in the HttpContext variable?


I'm trying to understand Dependency Injection in .NET Core. Can someone point me to where services added with AddScoped (in Startup.cs) are store at?

Is it the HttpContext variable?


Solution

  • Services configured in the IServiceCollection aren't really "stored" at least not in any lookup table that the application should be referencing directly!

    Adding an service using AddScoped, AddTransient or AddSingleton simply tells the framework how to produce a service within a scope. The framework will manage producing the service from the defintion when constructing managed components via Dependency Injection, or it will use a cached version when one already exists for that scope.

    For AddScoped, the framework will be using some sort of cache keyed by a request/connection id, but all you have to do is write a transient or scoped component (such as a Controller) and declare an instance of your scoped service as a dependency in the constructor.