In an ASP.NET Core Web API project, I use Task.Run to trigger a long running operation.
Here is the code:
[HttpPost("endpoint1")]
public ActionResult Endpoint1()
{
_ = Task.Run(async () =>
{
// create a new IoC container from scratch
// do some long running operation using the services from the container
});
return Ok();
}
I would have a few question to it:
- Will the running task not be disposed while it is running since there is no reference to it? Is it safe in this regard?
- Inside the
Runmethod I create a new IoC container containing services from scratch. Would there be a better alternative that would solve this issue?