Код: Выделить всё
class MyBackgroundService : BackgroundService
{
// ...
Dictionary _workers
public MyBackgroundService(/*a bunch of injected dependencies*/)
{
// store the injected dependencies in private fields
}
protected override Task ExecuteAsync(CancellationToken token)
{
while (true)
{
if (_myWatcher.GetNewConfigurations(out var newConfigurations)
{
foreach (var newConfiguration in newConfigurations)
{
_workers[newConfiguration.Id] = new MyWorker(/*pass in all the injected private fields*/);
}
}
// some code to remove workers that are no longer needed from the dictionary
// some code to break out of the loop when appropriate
}
}
// ...
}
Код: Выделить всё
MyBackgroundServiceКод: Выделить всё
class MyBackgroundService : BackgroundService
{
// ...
Dictionary _workers
private readonly IServiceProvider _serviceProvider;
public MyBackgroundService(IServiceProvider sp)
{
_serviceProvider = sp;
}
protected override Task ExecuteAsync(CancellationToken token)
{
while (true)
{
if (_myWatcher.GetNewConfigurations(out var newConfigurations)
{
foreach (var newConfiguration in newConfigurations)
{
_workers[newConfiguration.Id] = _serviceProvider.GetService();
}
}
// some code to remove workers that are no longer needed from the dictionary
// some code to break out of the loop when appropriate
}
}
// ...
}
The other obvious option is a factory pattern, in which I inject a MyWorkerFactory into MyBackgroundService, but that sort of just kicks the can down the road, where instead of MyBackgroundService taking in a bunch of dependencies it doesn't use, MyWorkerFactory does instead.
I thought at first simply внедрение класса MyWorker как временного решило бы проблему, пока я не понял, что Transient создает новый экземпляр для каждого экземпляра, который его запрашивает, но поскольку MyBackgroundService является одноэлементным, все равно будет только один MyWorker.
Как правильно это сделать?