I have the following constructor definition for my class:
public MyWorkerService(IConfiguration configuration, ILogger<MyWorkerService> logger, IWorkerService workerSerice)
I then have the following code to create a hosted service:
builder.Host.ConfigureServices(services =>
{
services.AddHostedService<MyWorkerService>();
});
This works great as I only have one implementation of IWorkerService. I now want two implementations of IWorkerService that both run.
I have tried the following:
builder.Services.AddHostedService(sp => new MyWorkerService(configuration, sp.GetRequiredService<ILogger<MyWorkerService>>(), sp.GetRequiredService<WorkerServiceA>()));
builder.Services.AddHostedService(sp => new MyWorkerService(configuration, sp.GetRequiredService<ILogger<MyWorkerService>>(), sp.GetRequiredService<WorkerServiceB>()));
WorkerServiceA is being called, WorkerServiceB is not. What am I doing wrong?
It looks like this is a design decision https://github.com/dotnet/runtime/issues/38751.
My round is to use generics to make unique names.
I can then use this: