How to queue background tasks in ASP.NET Web API

前端 未结 4 437
無奈伤痛
無奈伤痛 2020-12-08 22:10

I have a webapi that is designed to process reports in a queue fashion. The steps the application takes are as follows:

  • Receive content
  • Map the conte
4条回答
  •  旧巷少年郎
    2020-12-08 22:53

    I like @Todd's solution. Just for the sake of completeness, but there's another option not mentioned yet:

    HostingEnvironment.QueueBackgroundWorkItem(cancellationToken =>
    {
        // Some long-running job
    });
    

    Note:

    "When ASP.NET has to recycle, it will notify the background work (by setting a CancellationToken) and will then wait up to 30 seconds for the work to complete. If the background work doesn’t complete in that time frame, the work will mysteriously disappear."

    And avoid using service methods with an injected DbContext here as this won't work.

    Sources: MariusSchulz and StephenCleary

提交回复
热议问题