Код: Выделить всё
builder.Services.AddSingleton(p => new(p.GetService()!, "Singleton"));Код: Выделить всё
public interface IDisposableService : IDisposable, IAsyncDisposable { }
public class DisposableService : IDisposableService
{
private static int globalId = 0;
private int id;
private readonly ILogger logger;
private readonly string type;
public DisposableService(ILogger 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);
}
Код: Выделить всё
info: BlazorWebApp.DisposableService[0]
Singleton: 1 Service Instance Created
Подробнее здесь: https://stackoverflow.com/questions/788 ... completion