AddSingleton is an ASP.NET Core dependency-injection registration method. It tells the built-in DI container to use one instance of a service throughout the application. section 9.1 section 9.2
Use AddSingleton in these forms:
- Register a concrete type directly: builder.Services.AddSingleton<MessageFactory>(); The container creates and reuses the registered type. section 9.1
- Register an existing object: builder.Services.AddSingleton(new EmailServerSettings("smtp.server.com", 25)); The supplied object is always treated as a singleton. section 9.2
- Choose another lifetime when each request needs a newly created object; a factory lambda can be registered with AddScoped instead. section 9.2
Registering a shared service
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddSingleton<MessageFactory>(); WebApplication app = builder.Build(); The container can now create MessageFactory when a dependency requires it and reuse the same instance throughout the application.

