Search code examples
.net-corebackground-processasp.net-core-3.1azure-function-app

Correct implementation of background worker for Azure function app


What would be the correct way to implement a background worker, on an azure function app, http triggered? The process won't be in any way a long running one, just 2-3 rest calls (between 0,5 and 3 seconds), but enough to break the overall user experience.

Currently, I am using Task.Run(async ()). The same rest calls are used in other use-cases are normally available calls.

Normally I would deal with it by using a IHostedService, but it's not available in Azure function apps.


Solution

  • You would need to have a different trigger. Have your HTTP-triggered function place a queue message in a queue, and have the queue-triggered function execute the background work.

    This is the proper solution for request-extrinsic code anyway, even if you aren't using Functions.

    Note that your queue messages will be delivered at-least-once, so if your queue-triggered function isn't idempotent, then you'll need to de-duplicate them.