ASP.NET Core | Service Lifetimes

When Services are registered, there is a lifetime for every service. ASP.NET Core provides the following lifetimes.

  • Transient - Services with transient lifetime are created each time they are requested from service container. So it's best suited for stateless, light weight services.
  • Scoped - Services with scoped lifetime are created once per connection or client request. When using scoped service in middleware then inject the service via invoke or invokeAsync method. You should not inject the service via constructor injection as it treats the service behavior like Singleton.
  • Singleton - Service with singleton lifetime is created once when first time the service is requested. For subsequent requests same instance is served by service container.

Comments

Popular posts from this blog

ASP.NET Core | Change Token

ASP.NET Core | Open Web Interface for .NET (OWIN)

How will you improve performance of ASP.NET Core Application?