Как удалить службу Singleton в Blazor после завершения подачи заявки?C#

Место общения программистов C#
Anonymous
Как удалить службу Singleton в Blazor после завершения подачи заявки?

Сообщение Anonymous »

У меня есть некоторая служба, добавленная в службы Blazor как Singleton:

Код: Выделить всё

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
Разные источники говорят разное, но на самом деле я не вижу ожидаемого поведения DI в Blazor.

Подробнее здесь: https://stackoverflow.com/questions/788 ... completion

Вернуться в «C#»