Run a background task from a controller action in asp.net core 2 : <p>I am developing a web application with a REST Api using C# with asp.net core 2.0</p>

<p>What I want to achieve is when the client send a request to an endpoint I will run a background task separated from the client request context which will be ended if the task started successfully.</p>

<p>I know there is HostedService but the problem is that the HostedService starts when the server starts, and as far as I know there is no way to start the HostedService manually from a controller.</p>

<p>Here is a simple code that demonstrate the question.</p>

<pre><code>[Authorize(AuthenticationSchemes = "UsersScheme")]
public class UsersController : Controller
{

    [HttpPost]
    public async Task&lt;JsonResult&gt; StartJob([FromForm] string UserId, [FromServices] IBackgroundJobService backgroundService) {

           //check user account
           (bool isStarted, string data) result = backgroundService.Start();

           return JsonResult(result);
    }
}
</code></pre>
