aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
552 stars 312 forks source link

Unable to activate more than two IhostedService background services #1560

Closed saisworld closed 5 years ago

saisworld commented 5 years ago

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 {

    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;
    }
}`

In my startup.cs,I have registered like this -

services.AddHostedService<BackgroundServiceClass>(); services.AddHostedService<BackgroundServiceClass2>();

Tratcher commented 5 years ago

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.

saisworld commented 5 years ago

@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

Tratcher commented 5 years ago

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.

saisworld commented 5 years ago

Thanks Tratcher. that did the trick.