Equivalent of Ihostedservice in asp.net framework for background tasks Equivalent of Ihostedservice in asp.net framework for background tasks multithreading multithreading

Equivalent of Ihostedservice in asp.net framework for background tasks


I guess it's a bit too late, but for someone with the same issue..

Do you know Quartz.NET and Hangfire.io ?

Maybe one of those both could be a very useful tool in your situation.I used it in many applications, and never had any issue.

For example, in Quartz.Net, you first have to create a "job" (it's the term used for this kind of background services) by creating a class implementing IJob interface:

public class HelloJob : IJob{    public async Task Execute(IJobExecutionContext context)    {        await Console.Out.WriteLineAsync("Greetings from HelloJob!");    }}

Then, you have to define when you want to check that job, there are many ways (CRON for example) but we just gonna use a simple schedule here :

        StdSchedulerFactory factory = new StdSchedulerFactory();        IScheduler scheduler = await factory.GetScheduler();        await scheduler.Start();        // define the job        IJobDetail job = JobBuilder.Create<HelloJob>()            .WithIdentity("job1", "group1")            .Build();        // Trigger the job to run now, and then repeat every 20 seconds        ITrigger trigger = TriggerBuilder.Create()            .WithIdentity("trigger1", "group1")            .StartNow()            .WithSimpleSchedule(x => x                .WithIntervalInSeconds(20)                .RepeatForever())            .Build();        // Tell quartz to schedule the job using our trigger, DON'T FORGET THIS ONE        await scheduler.ScheduleJob(job, trigger);

You are in a micro services architecture based on windows service. You should be able to catch all "graceful" shutdown of your application. In these cases, you have to shutdown properly the Quartz scheduler :

await scheduler.Shutdown();

I personally really like these kinds of approach. It's reusable (you just have to schedule a new job here) and easy to get into that for any developer on your team.

I hope it will help you a bit with your issue.


ASP.NET has a native solution for running background tasks: HostingEnvironment.QueueBackgroundWorkItem method, found on System.Web.Hosting.

It works either for synchronous or async methods. Different from using a ThreadPool, QueueBackgroundWorkItem ensures that ASP.NET runtime will delay the application shutdown in case there are any pending work items running.

From the official documentation:

Differs from a normal ThreadPool work item in that ASP.NET can keep track of how many work items registered through this API are currently running, and the ASP.NET runtime will try to delay AppDomain shutdown until these work items have finished executing. This API cannot be called outside of an ASP.NET-managed AppDomain. The provided CancellationToken will be signaled when the application is shutting down.

Here's an example of how it can be used:

using System;using System.Diagnostics;using System.Threading;using System.Threading.Tasks;using System.Web.Hosting;using System.Web.Http;namespace BackgroundWorkItemExample.Controllers{    public class HomeController : ApiController    {        [HttpPost]        public IHttpActionResult Execute()        {            Debug.WriteLine("Doing something.");            HostingEnvironment.QueueBackgroundWorkItem(ct => FireAndForgetMethodAsync());            HostingEnvironment.QueueBackgroundWorkItem(ct => FireAndForgetMethod());            HostingEnvironment.QueueBackgroundWorkItem(ct => FaultyFireAndForgetMethod());            Debug.WriteLine("I don't care for that stuff running in the background. I'm out.");            return Ok("It still returns, regardless of exceptions.");        }        private async Task FireAndForgetMethodAsync()        {            Debug.WriteLine("Started running an asynchronous background work item...");            await Task.Delay(5000); // Pretend we are doing something for 5s            Debug.WriteLine("Finished running that.");        }        private void FireAndForgetMethod()        {            Debug.WriteLine("Started running a background work item...");            Thread.Sleep(5000); // Pretend we are doing something for 5s            Debug.WriteLine("Finished running that.");        }        private void FaultyFireAndForgetMethod()        {            throw new Exception("I'm failing just to make a point.");        }    }}

The output would be:

Doing something.I don't care for that stuff running in the background. I'm out.Started running a background work item...Started running an asynchronous background work item...Exception thrown: 'System.Exception' in WebApplication1.dllAn exception of type 'System.Exception' occurred in WebApplication1.dll but was not handled in user codeI'm failing just to make a point.Finished running that.Finished running that.


One of the safest ways to proceed would be to use a Thread.You can fire the thread ans let it run.

If you need the thread to get killed in case the App closed, You can add the isBackground parameter.
Otherwise, the thread should continue to run until its work is finished

Thread backgroundThread = new Thread(() => {    Console.WriteLine("I'll run until the app Stops or I finish working");    LongRunningJob();}, IsBackground = true);Thread foregroundThread = new Thread(() => {    Console.WriteLine("I'll stop when I'm finished");    LongRunningJob();}backgroundThread.Start();foregroundThread.Start()