Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Unable to activate more than two IhostedService background services #1560

Closed
saisworld opened this issue Oct 5, 2018 · 4 comments
Closed

Comments

@saisworld
Copy link

saisworld commented Oct 5, 2018

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
Copy link
Member

Tratcher commented Oct 5, 2018

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
Copy link
Author

@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
Copy link
Member

Tratcher commented Oct 5, 2018

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
Copy link
Author

Thanks Tratcher. that did the trick.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants