Running background tasks periodically in an ASP.NET Core RC2 application
Asked Answered
K

6

11

I'm working on an ASP.NET Core RC2 application. There is a requirement for this application to periodically invoke certain tasks, such as sending emails or invoking specific business logic.

I'm aware that there are third-party libraries such as Hangfire or Quartz.NET that provide this scheduling functionality out-of-the-box. However I don't think either of these currently support ASP.NET Core RC2. Are there are any other options available that are compatible with ASP.NET Core RC2?

If not, I guess a possible option is to use one of these third-party libraries from within a separate windows service, which could then target a supported version of .NET. This service could then periodically make requests to the ASP.NET application through its Web API in order to invoke the tasks.

However I'd prefer not to have a separate service as it increases the number of moving parts and complicates the deployment of our application.

Kusin answered 31/5, 2016 at 11:22 Comment(2)
Where are you running the application? Azure? If you use Azure App service you can use Worker roles to deploy the background applications toCapybara
@Capybara thanks for your response. Yes it will be deployed to Azure. Are you suggesting worker roles as an alternative to using Hangfire/Quartz.NET, or are you suggesting that Hangfire or Quartz.NET would be used within a worker role? Is a WebJob a feasible alternative to a worker role?Kusin
H
10

A manual approach:

  • Create a Service containing the long-running code, preferably async
  • Add a while loop to the service with pausing (also preferably async)
  • Call the long running method of the service class in a new thread with Task.Run(() => ... or in the thread pool (or in a legacy Thread).
  • Create an instance of the class in Startup.cs
  • Use services.AddSingleton(MyServiceInstance); in Configure(... in Startup.cs
Heighttopaper answered 17/1, 2017 at 16:31 Comment(0)
R
3
  • If you're targeting the full .NET CLR then any library that worked before will work now. That hasn't changed in RC2. Azure WebJobs is another solution, in addition to those that you already mentioned.

  • For CoreCLR I'm not aware of any framework that's already compatible. You might have to come up with your own solution or break your application in two parts by either making having all the scheduled code in full .NET or by making a small app in full .NET that invokes a CoreCLR one.

Rideout answered 31/5, 2016 at 16:40 Comment(0)
H
3

ASP.NET Core support to run background tasks with hosted services, see this doc. It has an interface named IHostedService for user-defined Hosted services. The interface defines two methods for objects that are managed by the host:

  • StartAsync(CancellationToken) - Called after the server has started and IApplicationLifetime.ApplicationStarted is triggered.
  • StopAsync(CancellationToken) - Triggered when the host is performing a graceful shutdown.

You can also inherit the abstract class BackgroundService by implementing the ExecuteAsync method like this:

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    stoppingToken.Register(() =>
            _logger.LogDebug($"Background task is stopping."));

    while (!stoppingToken.IsCancellationRequested)
    {
        _logger.LogDebug($"Background task doing work.");

        DoTask();

        await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
    }

}

After defining your hosted services, you need to add them to ServiceCollection as singleton. Then these background tasks will be running within the asp.net core application.

Hinshelwood answered 17/5, 2018 at 17:25 Comment(0)
S
1

We use a simple "task runner" application that takes command line parameters to determine which web/Windows service to call. Then use Windows Task Scheduler to call the "task runner" application. So it doesn't matter what the service is or what technology it was written in.

Shellyshelman answered 31/5, 2016 at 11:42 Comment(1)
What is the purpose of “task runner”? In Windows Task Scheduler you can directly specify service path and parameters.Ailina
A
1

You can always give Quartz v3 build a go. It's already running on CoreCLR and tests are pretty much passing. You need to build from sources yourself though using Visual Studio. See the v3 branch.

Biggest caveat at the moment is the incompatible serialization as v2 can only serialize binary to database (job data maps etc) and v3 only supports JSON as serialization format. The story might change as they are planning to bring BinaryFormatter back after all.

So I'd say if you are happy using just RAMJobStore without database persistence you should be golden. It will probably work better than hand-crafted solution.

Allveta answered 1/6, 2016 at 7:2 Comment(1)
Thank you - I'll certainly give this a try. We used Quartz in my previous job and it worked really well for us.Kusin
A
1

Consider to use https://github.com/fluentscheduler/FluentScheduler.

Example of job is very simple

JobManager.AddJob(() => Console.WriteLine("My job!"),
                 (s) => s.ToRunEvery(5).Seconds());
Ailina answered 2/9, 2018 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.