Singleton
Configuration Settings — one instance for the entire application.
When to use: shared app-wide state such as configuration.
Be careful with mutable state — changes affect every request.
Where do the three IDs come from?
When this page loads, ASP.NET Core builds the page model by calling its constructor.
The container resolves IConfigurationSettings twice in the page, and also injects
SampleService. Because this service is a singleton, SampleService receives the same instance as the page — one object shared for the entire application.
Page constructor — SingletonModel
public SingletonModel( IConfigurationSettings firstInjection, // parameter 1: first injection IConfigurationSettings secondInjection, // parameter 2: second injection SampleService sampleService) // parameter 3: via SampleService { FirstInjectionId = firstInjection.InstanceId; SecondInjectionId = secondInjection.InstanceId; SampleServiceId = sampleService.ConfigurationSettings.InstanceId; }
SampleService constructor
public SampleService( IConfigurationSettings configurationSettings, ...) { ConfigurationSettings = configurationSettings; } // This page reads: sampleService.ConfigurationSettings.InstanceId
Result in this request
| Resolution | Constructor parameter | Instance ID |
|---|---|---|
| First injection | firstInjection |
bdce1ea1-9966-4f35-b119-4354debd8983 |
| Second injection | secondInjection |
bdce1ea1-9966-4f35-b119-4354debd8983 |
Via SampleService |
sampleService.ConfigurationSettings |
bdce1ea1-9966-4f35-b119-4354debd8983 |
All IDs match within this request. Refresh the page or open a second browser tab —
the ID stays the same until you restart the application.
Registration in Program.cs:
builder.Services.AddSingleton<IConfigurationSettings, ConfigurationSettings>();