Inject services with different lifetimes with .NET

Published: Monday 11 September 2023

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?