Do some work after the response in ASP.NET Core

前端 未结 5 1677
梦如初夏
梦如初夏 2021-02-08 03:31

I have an ASP.NET Core website, using EFCore. I would like to do some work like logging to the database, but after having sent the response to the user in order to answer faster

5条回答
  •  孤城傲影
    2021-02-08 03:34

    There's no out of the box way to do what you want.

    But, here's a possible approach:

    1. Have a queue and a worker (thread or process)
    2. Just before the request is sent back to the client, add a message in that queue
    3. The worker will pick up that message at some point in the future, and process it.

    Since the worked runs somewhere else and not on the request thread, the server can complete the request thread and the worker can do what's left.

提交回复
热议问题