You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
I'm trying to activate two background services both implementing IHostedService interface. However, during runtime only one gets activated. The first service that was registered in the startup.cs gets activated. Please advise.
public class BackgroundServiceClass2 : IHostedService {
public BackgroundServiceClass2()
{
//Constructor’s parameters validations...
}
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine($"BackgroundServiceClass 2 is starting." + DateTime.Now);
cancellationToken.Register(() =>
Console.WriteLine($" Background ServiceClass task is stopping."));
while (!cancellationToken.IsCancellationRequested)
{
Console.WriteLine($"BackgroundServiceClass 2 task doing background work." + DateTime.Now);
// This eShopOnContainers method is quering a database table
// and publishing events into the Event Bus (RabbitMS / ServiceBus)
//CheckConfirmedGracePeriodOrders();
Thread.Sleep(5000);
}
Console.WriteLine($"BackgroundServiceClass 2 background task is stopping.");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine($"BackgroundServiceClass 2 background task is stopping permanently.");
return Task.CompletedTask;
}
}`
Second Ihostedservice -
` public class BackgroundServiceClass
: IHostedService
{
public BackgroundServiceClass()
{
//Constructor’s parameters validations...
}
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine($"BackgroundServiceClass is starting." + DateTime.Now);
cancellationToken.Register(() =>
Console.WriteLine($" Background ServiceClass task is stopping."));
while (!cancellationToken.IsCancellationRequested)
{
Console.WriteLine($"BackgroundServiceClass task doing background work." + DateTime.Now);
// This eShopOnContainers method is quering a database table
// and publishing events into the Event Bus (RabbitMS / ServiceBus)
//CheckConfirmedGracePeriodOrders();
Thread.Sleep(5000);
}
Console.WriteLine($"BackgroundServiceClass background task is stopping.");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine($"BackgroundServiceClass background task is stopping permanently.");
return Task.CompletedTask;
}
}`
StartAsync is intended to be non-blocking. Schedule any work you need to and then let StartAsync finish. It's waiting for the first one to finish starting before it tries to start the next one.
@Tratcher I believe Backgroundservices /IHostedService is intended for the long running jobs. If it has to finish off the work then its no longer a long backgroundservice service.
Or .Net core doesnt allow running multiple backgroundservices using IHostedservice
The background service doesn't need to complete, only the StartAsync method. Here's one example where you override ExecuteAsync, not StartAsync. In that example it's still important that ExecuteAsync is actually async or it can block StartAsync.
Hi,
I'm trying to activate two background services both implementing IHostedService interface. However, during runtime only one gets activated. The first service that was registered in the startup.cs gets activated. Please advise.
public class BackgroundServiceClass2 : IHostedService {
Second Ihostedservice -
` public class BackgroundServiceClass
: IHostedService
{
In my startup.cs,I have registered like this -
services.AddHostedService<BackgroundServiceClass>(); services.AddHostedService<BackgroundServiceClass2>();
The text was updated successfully, but these errors were encountered: