ASP.NET Core has many features introduced by Microsoft like the below which could help make applications faster and more robust:
- Cross Platform so that it can run on Windows, Max and Linux
- In builds support of Dependency Injection – it means there is no need to write extra code for it
- Very good support of Asynchronous Programming which makes applications faster
- Support of WebSocket and SingalR
- 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,
- Transient Service
- Scoped Service
- Singleton Service
Let’s have a look in depth at how it works.
Let’s have a look at the basic examples of it.
Transient Service
- //1st Step: Create the required service
- public interface IMyFirstTransientService
- {
- string SaysHello();
- }
-
- public class MyFirstTransientService: IMyFirstTransientService
- {
- public string ShowMessage()
- {
- return "How are you my friend? Transient";
- }
- }
-
- //2nd Step: Add above created service to Service container as below
- public void ConfigureServices(IServiceCollection services)
- {
-
- services.AddTransient<IMyFirstTransientService, MyFirstTransientService>();
-
- }
-
- //3rd Step: Use above created service as a dependency in the specific or required controller
- public class HomeController: Controller
- {
- IMyFirstTransientService _myFirstTransientService;
- public HomeController(IMyFirstTransientService myFirstTransientService)
- {
- _myFirstTransientService = myFirstTransientService;
- }
- }
Scoped Service
- //1st Step: Create the required service
- public interface IMyFirstScopedService
- {
- string SaysHello();
- }
-
- public class MyFirstScopedService: IMyFirstScopedService
- {
- public string ShowMessage()
- {
- return "How are you my frient? Scoped";
- }
- }
-
- //2nd Step: Add above created service to Service container as below
- public void ConfigureServices(IServiceCollection services)
- {
-
- services.AddScoped<IMyFirstScopedService, MyFirstScopedService>();
-
- }
-
- //3rd Step: Use above created service as a dependency in the specific or required controller
- public class HomeController: Controller
- {
- IMyFirstScopedService _myFirstScopedService;
- public HomeController(IMyFirstScopedService myFirstScopedService)
- {
- _myFirstScopedService = myFirstScopedService;
- }
- }
- //1st Step: Create the required service
- public interface IMyFirstSingletonService
- {
- string SaysHello();
- }
-
- public class MyFirstSingletonService: IMyFirstSingletonService
- {
- public string ShowMessage()
- {
- return "How are you my frient? Singleton";
- }
- }
-
- //2nd Step: Add above created service to Service container as below
- public void ConfigureServices(IServiceCollection services)
- {
-
- services.AddSingleton<IMyFirstSingletonService, MyFirstSingletonService>();
-
- }
-
- //3rd Step: Use above created service as a dependency in the specific or required controller
- public class HomeController: Controller
- {
- IMyFirstSingletonService _myFirstSingletonService;
- public HomeController(IMyFirstSingletonService myFirstSingletonService)
- {
- _myFirstSingletonService = myFirstSingletonService;
- }
- }