I have some service added to Blazor services as Singleton:
builder.Services.AddSingleton<IDisposableService, DisposableService>(p => new(p.GetService<ILogger<DisposableService>>()!, "Singleton"));
public interface IDisposableService : IDisposable, IAsyncDisposable { }
public class DisposableService : IDisposableService
{
private static int globalId = 0;
private int id;
private readonly ILogger<DisposableService> logger;
private readonly string type;
public DisposableService(ILogger<DisposableService> logger, string type)
{
this.logger = logger;
this.type = type;
id = ++globalId;
logger.LogInformation("{id}: {type} Service Instance Created", type, id);
}
public void Dispose() => logger.LogWarning("{id}: {type} Service Disposed!", type, id);
public async ValueTask DisposeAsync() => logger.LogWarning("{id}: {type} Service Disposed Asynchronously!", type, id);
}
I expected that upon application completion my service should be disposed, but it is not.
info: BlazorWebApp.DisposableService[0]
Singleton: 1 Service Instance Created
Different sources say different things, but in fact, I don’t see the expected DI behavior in Blazor.