Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to dispose my Singleton service in Blazor upon application completion?

$
0
0

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.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>