Dependency Injection
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies.
Registration
Register your dependencies in ConfigureServices
.
Transient
Transient lifetime services (AddTransient
) are created each time they're requested from the service container. This lifetime works best for lightweight, stateless services.
Scoped
Scoped lifetime services (AddScoped
) are created once per client request (connection).
Singleton
Singleton lifetime services (AddSingleton
) are created the first time they're requested (or when Startup.ConfigureServices
is run and an instance is specified with the service registration). Every subsequent request uses the same instance.
Usage
Last updated