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 bef643c1-1d9d-4780-b9e8-bc362d7f1bf8
Second injection secondInjection bef643c1-1d9d-4780-b9e8-bc362d7f1bf8
Via SampleService sampleService.ConfigurationSettings bef643c1-1d9d-4780-b9e8-bc362d7f1bf8
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>();

← Back to overview