In an .NET Core 3.1 (Console) application, is there a way to launch services, added by AddHostedService
in parallel?
Actually the two services I add seem to be launched in synchronous mode (one after other)
My code is the following:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host
.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, confBuilder) =>
{
confBuilder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
})
.ConfigureServices((hostContext, services) =>
{
if (args.Contains(Args.Contoso))
{
services.AddHostedService(provider =>
new ContosoService(
provider.GetService<ILogger<ContosoService>>(),
provider.GetService<IContosoRepository>(),
mode));
}
// if there also Alonso in the list, make them run in parallel !
if (args.Contains(Args.Alonso))
{
services.AddHostedService(provider =>
new AlonsoService(
provider.GetService<ILogger<AlonsoService>>(),
provider.GetService<IAlonsoRepository>(),
mode));
}
});
knowing that both services are IHostedService
public class AlonsoService : IHostedService {...}
public class ContosoService : IHostedService {...}
question from:
https://stackoverflow.com/questions/65599001/launch-addhostedservice-services-in-parallel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…