Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long running method in controller action - MVC [duplicate]

I have a method similar to the following in WebAPI - MVC

public IActionResult Post(Model model)
{
      ALongRunningMethod();
      return Ok();
}

I have two options.

  1. Wait for the method to complete. But, what if its too long ? Doesn't the API timeout ?
  2. Run that in a separate thread using Tasks but return Ok (200 HTTP Code) immediately ? What if some exception occur in that long running method ?

Totally confused. What should I do in this case ?

like image 742
now he who must not be named. Avatar asked Jan 10 '23 18:01

now he who must not be named.


1 Answers

Starting from .Net 4.5.2, you now have QueueBackgroundWorkItem to deal with long running operations in ASP.Net.

like image 97
ken2k Avatar answered Jan 18 '23 19:01

ken2k