26

I have an ASP.NET Core web app, with WebAPI controllers. All I am trying to do is, in some of the controllers, be able to kick off a process that would run in the background, but the controller should go ahead and return before that process is done. I don't want the consumers of the service to have to wait for this job to finish.

I have seen all of the posts about IHostedService and BackgroundService, but none of them seem to be what I want. Also, all these examples show you how to set things up, but not how to actually call it, or I am not understanding some of it.

I tried these, but when you register an IHostedService in Startup, it runs immediately at that point in time. This is not what I want. I don't want to run the task at startup, I want to be able to call it from a controller when it needs to. Also, I may have several different ones, so just registering services.AddHostedService() won't work because I might have a MyServiceB and MyServiceC, so how do I get the right one from the controller (I can't just inject IHostedService)?

Ultimately, everything I have seen has been a huge, convoluted mess of code for something that seems like it should be such a simple thing to do. What am I missing?

RebelScum
  • 549
  • 2
  • 5
  • 19
  • Have you tried with asynchronous methods? (Docs: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/) – mrbitzilla Jun 05 '20 at 19:22
  • You don't call `IHostedService` you send messages to it some way. One of the default examples - [Queued background tasks](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio#queued-background-tasks) in the docs (in this case `Func workItem` is message). – Guru Stron Jun 05 '20 at 19:28
  • "it should be such a simple thing to do" - it would be nice, but it simply is not a simple thing. It is hard to making sure that code predictably completes when there is *zero guarantees* that process where code tries to run will stay around long enough for that code to finish. – Alexei Levenkov Jun 05 '20 at 19:28
  • Does this answer your question? [Run a background task from a controller action in ASP.NET Core](https://stackoverflow.com/questions/49813628/run-a-background-task-from-a-controller-action-in-asp-net-core-2) – janw Feb 21 '21 at 15:25

3 Answers3

16

You have the following options:

  1. IHostedService classes can be long running methods that run in the background for the lifetime of your app. In order to make them to handle some sort of background task, you need to implement some sort of "global" queue system in your app for the controllers to store the data/events. This queue system can be as simple as a Singleton class with a ConcurrentQueue that you pass in to your controller, or something like an IDistributedCache or more complex external pub/sub systems. Then you can just poll the queue in your IHostedService and run certain operations based on it. Here is a microsoft example of IHostedService implementation for handling queues https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio#queued-background-tasks Note that the Singleton class approach can cause issues in multi-server environments. Example implementation of the Singleton approach can be like:
// Needs to be registered as a Singleton in your Startup.cs
public class BackgroundJobs {
  public ConcurrentQueue<string> BackgroundTasks {get; set;} = new ConcurrentQueue<string>();
}

public class MyController : ControllerBase{
  private readonly BackgroundJobs _backgroundJobs;
  public MyController(BackgroundJobs backgroundJobs) {
    _backgroundJobs = backgroundJobs;
  }

  public async Task<ActionResult> FireAndForgetEndPoint(){
    _backgroundJobs.BackgroundTasks.Enqueue("SomeJobIdentifier");
  }
}

public class MyBackgroundService : IHostedService {
  private readonly BackgroundJobs _backgroundJobs;
  public MyBackgroundService(BackgroundJobs backgroundJobs)
  {
    _backgroundJobs = backgroundJobs;
  }

  public void StartAsync(CancellationToken ct)
  {
    while(!ct.IsCancellationRequested)
    {
      if(_backgroundJobs.BackgroundTasks.TryDequeue(out var jobId))
      {
        // Code to do long running operation
      }
    Task.Delay(TimeSpan.FromSeconds(1)); // You really don't want an infinite loop here without having any sort of delays.
    }
  }
}
  1. Create a method that returns a Task, pass in a IServiceProvider to that method and create a new Scope in there to make sure ASP.NET would not kill the task when the controller Action completes. Something like
IServiceProvider _serviceProvider;

public async Task<ActionResult> FireAndForgetEndPoint()
{
  // Do stuff
  _ = FireAndForgetOperation(_serviceProvider);
  Return Ok();
}

public async Task FireAndForgetOperation(IServiceProvider serviceProvider)
{
  using (var scope = _serviceProvider.CreateScope()){
    await Task.Delay(1000);
    //... Long running tasks
  }
}

Update: Here is the Microsoft example of doing something similar: https://learn.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-3.1#do-not-capture-services-injected-into-the-controllers-on-background-threads

NavidM
  • 1,515
  • 1
  • 16
  • 27
  • 1
    I have seen that "Queued Background Tasks" example from Microsoft, but I can't quite figure out how to change it to suit my purposes. I don't want a MonitorLoop, and I can't quite figure out how to queue a task manually from my Controller. Actually, at this point, I am very tempted to just use Task.Run like you originally suggested, because I have a feeling the only reason it's a "bad practice" is that it still takes up the same CPU cycles. But in my case, that is OK. I am fine with it taking up the same cycles, I just want to return to the user before kicking this off. – RebelScum Jun 05 '20 at 20:10
  • `MonitorLoop` is an example in there. You do not need to implement it. The only thing you need is a class that has at least one property that is your task queue, and you can register that class as a singleton and use the dependency injection to inject it both to your controller and your `BackgroundService`. – NavidM Jun 05 '20 at 20:16
  • 3
    @RebelScum `Task.Run` is a bad practice because it means that you're not using the DI to its fullest. In fact, if you use this in an HttpRequest scope, in your background task, you'll perhaps use a disposed object and get an exception. Thus, you're introducing uncertain behavior to your app – Emy Blacksmith Nov 03 '20 at 11:30
  • 1
    Yeah you have to use create the scope and manually retrieve objects from it, otherwise, things might be disposed as Ramen said. Personally, I think it is a code smell but it is a way to do it. – NavidM Nov 04 '20 at 18:04
15

As I understand from your question you want to create a fire and forget task like logging to database. In this scenario you don't have to wait for log to be inserted database. It also took much of my time to discover an easily implementable solution. Here is what I have found:

In your controller parameters, add IServiceScopeFactory. This will not effect the request body or header. After that create a scope and call your service over it.

[HttpPost]
public IActionResult MoveRecordingToStorage([FromBody] StreamingRequestModel req, [FromServices] IServiceScopeFactory serviceScopeFactory)
{
    // Move record to Azure storage in the background
    Task.Run(async () => 
    {
        try
        {
            using var scope = serviceScopeFactory.CreateScope();
            var repository = scope.ServiceProvider.GetRequiredService<ICloudStorage>();
            await repository.UploadFileToAzure(req.RecordedPath, key, req.Id, req.RecordCode);
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
        }
    });
    return Ok("In progress..");
}

After posting your request, you will immediately receive In Progress.. text but your task will run in the background.

One more thing, If you don't create your task in this way and try to call database operations you will receive an error like this which means your database object is already dead and you are trying to access it;

Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.\r\nObject name: 'DBContext'.

My code is based on Repository pattern. You should not forget to inject service class in your Startup.cs

services.AddScoped<ICloudStorage, AzureCloudStorage>();

Find the detailed documentation here.

Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38
6

What is the simplest way to run a single background task from a controller in .NET Core?

I don't want the consumers of the service to have to wait for this job to finish.

Ultimately, everything I have seen has been a huge, convoluted mess of code for something that seems like it should be such a simple thing to do. What am I missing?

The problem is that ASP.NET is a framework for writing web services, which are applications that respond to requests. But as soon as your code says "I don't want the consumers of the service to have to wait", then you're talking about running code outside of a request (i.e., request-extrinsic code). This is why all solutions are complex: your code has to bypass/extend the framework itself in an attempt to force it to do something it wasn't designed to do.

The only proper solution for request-extrinsic code is to have a durable queue with a separate background process. Anything in-process (e.g., ConcurrentQueue with an IHostedService) will have reliability problems; in particular, those solutions will occasionally lose work.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810