ASP.NET Core Long Running/Background Task

Asp.Net core long running/background task

Is the following a correct pattern to implement long running background work in Asp.Net Core?

Yes, this is the basic approach to start long-running work on ASP.NET Core. You should certainly not use Task.Run/StartNew/LongRunning - that approach has always been wrong.

Note that your long-running work may be shut down at any time, and that's normal. If you need a more reliable solution, then you should have a separate background system outside of ASP.NET (e.g., Azure functions / AWS lambdas). There are also libraries like Hangfire that give you some reliability but have their own drawbacks.

Update: I've written a blog series on how to implement the more reliable approach, using a durable queue with a separate background system. In my experience, most developers need the more reliable approach because they don't want their long-running work to be lost.

Disadvantages due to many long running (program execution time) background tasks in C#

There are a few possible problems with many background tasks

Threadpool exhaustion

background tasks are normally run on the threadpool. This has only so many threads. There is a mechanism for adding and removing threads, but this is purposely designed to be slow. When creating tasks you can specify TaskCreationOptions.LongRunning, this hints the pool that additional threads may be needed.

Resource usage

Tasks are run on threads, and threads need some resources to run, notably some stack memory. However, your probably does not do actual work most of the time, so if they use await while waiting for something to happen, no thread is used, and any resources can be used for something else.

Timers

If most background tasks are for performing periodic tasks, then timers may be a better alternative.

Scheduling

If background tasks need to keep most resources free for the foreground work, then you might consider using a LimitedConcurrencyLevelTaskScheduler to ensure the background tasks only uses up a fixed number of threads.

c# asp.net core awaiting long running task stops after period of time

it will just stop at some time. And it will stop even if I wrap it in IServiceProvider.CreateScope and await it.

Yes. That's the problem with in-memory background services. They can be stopped at any time, because they're hosted in an ASP.NET process that determines it's safe to shut down when requests are complete. ASP.NET will actually request a shutdown and then wait for a while for the services to complete, but there's also a timer where they'll be forced out if they don't complete within 10 minutes or so.

The bottom line is that shutdowns are normal. Any code that assumes it can run indefinitely in ASP.NET is inherently buggy.

The only thing I still didn't try and I'm trying to avoid it, is creating dedicated .net application that would just read queue and do what it's supposed to do.

That is the only reliable solution.



Related Topics



Leave a reply



Submit