Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DI Service Lifetimes Showcase

This project demonstrates the different lifetimes of services in Dependency Injection (DI) using a simple console application. It includes examples of Singleton, Scoped, and Transient service lifetimes.


Services

  • SingletonService: This service is created once and shared across the entire application. If the app requires singleton behavior, allow the service container to manage the service's lifetime. Don't implement the singleton design pattern and provide code to dispose of the singleton. Services should never be disposed by code that resolved the service from the container. Singleton services are disposed when the ServiceProvider is disposed on application shutdown. Because memory isn't released until the app shuts down, consider memory use with a singleton service. Register this service with AddSingleton.
  • ScopedService: This service is created once per client request (connection). Register this service with AddScoped. Scoped services are disposed at the end of the request. A scoped service should never be resolved directly from a singleton > this causes the scoped service to behave like a singleton.
  • TransientService: This service is created each time it is requested from the service container. Register this service with AddTransient. Transient services are disposed at the end of the request.

Application Structure

Server

  • ASP .NET Core, Net 10.0, C# 14.0
  • Program.cs: The main entry point of the application where the DI container is configured and services are registered.
  • Services: Contains the implementation of the Singleton, Scoped, and Transient services.
  • Controllers: Contains the API controllers that demonstrate the use of the different service lifetimes.

Client

  • Angular 21, TypeScript
  • A simple Angular application that makes HTTP requests to the ASP .NET Core server to demonstrate the behavior of the different service lifetimes.

Running the Application

  1. Clone the repository.
  2. Navigate to the server project and run it using dotnet run.
  3. Navigate to the client project and run it using ng serve.
  4. Open a browser and navigate to the client's url.
  5. Use the provided UI to make requests to the server and observe the behavior of the different service lifetimes in the console output.

Releases

Packages

Contributors

Languages