As the title states, when I register multiple instances of IHostedService, it calls StartAsync twice on the first instance, but not the second, but it does call both constructors.
Program.cs
services.AddSingleton<IHostedService, ProductService>(provider => (ProductService)provider.GetService<IProductService>()!);
services.AddSingleton<IProductService, ProductService>();
services.AddSingleton<IHostedService, ProductService>(provider => (ProductService)provider.GetService<IProductService>()!);
services.AddSingleton<IProductService, ProductService>();
ProductService.cs
public class ProductService : IProductService, IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken) { }
public async Task StopAsync(CancellationToken cancellationToken) { }
}
How can I solve this? I need multiple instances of ProductService (name changed for simplicity).