- Home
- .NET coding challenges
- Inject services with different lifetimes with .NET
Inject services with different lifetimes with .NET
With dependency injection in ASP.NET Core, there are three different service lifetimes that can be used:
- Singleton - The lifetime will last for the lifetime of the application.
- Scoped - The lifetime is initalised when a HTTP request has been made and is disposed of when the HTTP response has been sent. It can also be defined explicitly.
- Transient - The lifetime is initalised every time a service is injected.
Most services can be injected directly into other services regardless of their lifetime. However, there is scenario where a service with a particular lifetime cannot inject a service with another service lifetime.
Can you find out which service lifetimes they are?
Give us your anonymous feedback regarding this page, or any other areas of the website.