Service Lifetime Dependency Injection - ASP.NET Core

ASP.NET Core has many features introduced by Microsoft like the below which could help  make applications faster and more robust:
  1. Cross Platform so that it can run on Windows, Max and Linux
  2. In builds support of Dependency Injection – it means there is no need to write extra code for it
  3. Very good support of Asynchronous Programming which makes applications faster
  4. Support of WebSocket and SingalR
  5. And many more….
Today we will have a look in brief at how in built Dependency Injection works in Asp.NET Core with different lifetimes so that the created instance gets disposed/flushed by its own based on a specific lifetime.
 
There are 3 types of lifetimes supported by ASP.NET Core for the dependency injection,
  1. Transient Service
  2. Scoped Service
  3. Singleton Service
Let’s have a look in depth at how it works.
Service Lifetime Dependency Injection - ASP.NET Core
 
Let’s have a look at the basic examples of it.
 
Transient Service
  1. //1st Step: Create the required service    
  2. public interface IMyFirstTransientService  
  3. {  
  4.   string SaysHello();  
  5. }  
  6.    
  7. public class MyFirstTransientService: IMyFirstTransientService  
  8. {  
  9.  public string ShowMessage()  
  10.  {  
  11.    return "How are you my friend? Transient";  
  12.  }  
  13. }  
  14.   
  15. //2nd Step: Add above created service to Service container as below   
  16. public void ConfigureServices(IServiceCollection services)  
  17. {  
  18.  // .. other code  
  19.  services.AddTransient<IMyFirstTransientService, MyFirstTransientService>();  
  20.  //.. other code  
  21. }  
  22.   
  23. //3rd Step: Use above created service as a dependency in the specific or required controller    
  24. public class HomeController: Controller  
  25. {  
  26.  IMyFirstTransientService _myFirstTransientService;  
  27.  public HomeController(IMyFirstTransientService myFirstTransientService)  
  28.  {  
  29.    _myFirstTransientService = myFirstTransientService;  
  30.  }  
  31. }  
 Scoped Service
  1. //1st Step: Create the required service    
  2. public interface IMyFirstScopedService  
  3. {  
  4.   string SaysHello();  
  5. }  
  6.    
  7. public class MyFirstScopedService: IMyFirstScopedService  
  8. {  
  9.  public string ShowMessage()  
  10.  {  
  11.    return "How are you my frient? Scoped";  
  12.  }  
  13. }  
  14.   
  15. //2nd Step: Add above created service to Service container as below    
  16. public void ConfigureServices(IServiceCollection services)  
  17. {  
  18.  // .. other code  
  19.  services.AddScoped<IMyFirstScopedService, MyFirstScopedService>();  
  20.  //.. other code  
  21. }  
  22.   
  23. //3rd Step: Use above created service as a dependency in the specific or required controller   
  24. public class HomeController: Controller  
  25. {  
  26.  IMyFirstScopedService _myFirstScopedService;  
  27.  public HomeController(IMyFirstScopedService myFirstScopedService)  
  28.  {  
  29.    _myFirstScopedService = myFirstScopedService;  
  30.  }  
  31. }  
Singleton Service 
  1. //1st Step: Create the required service    
  2. public interface IMyFirstSingletonService  
  3. {  
  4.   string SaysHello();  
  5. }  
  6.    
  7. public class MyFirstSingletonService: IMyFirstSingletonService  
  8. {  
  9.  public string ShowMessage()  
  10.  {  
  11.    return "How are you my frient? Singleton";  
  12.  }  
  13. }  
  14.   
  15. //2nd Step: Add above created service to Service container as below    
  16. public void ConfigureServices(IServiceCollection services)  
  17. {  
  18.  // .. other code  
  19.  services.AddSingleton<IMyFirstSingletonService, MyFirstSingletonService>();  
  20.  //.. other code  
  21. }  
  22.   
  23. //3rd Step: Use above created service as a dependency in the specific or required controller    
  24. public class HomeController: Controller  
  25. {  
  26.  IMyFirstSingletonService _myFirstSingletonService;  
  27.  public HomeController(IMyFirstSingletonService myFirstSingletonService)  
  28.  {  
  29.    _myFirstSingletonService = myFirstSingletonService;  
  30.  }  
  31. }