koenbeuk / EntityFrameworkCore.Triggered

Triggers for EFCore. Respond to changes in your DbContext before and after they are committed to the database.
MIT License
554 stars 29 forks source link

Run like Fire-and-forget tasks #178

Closed iAmarokk closed 1 year ago

iAmarokk commented 1 year ago

Hi all, Can the trigger work as an Fire-and-forget tasks in Hangfire ?

koenbeuk commented 1 year ago

If you're asking if a trigger can enqueue a job in hangfire then most certainly yes. If you're asking if you can do work in the background invoked by a trigger, without using hangfire then that is also a yes with some caveats. Can you clarify your question?

iAmarokk commented 1 year ago

Both options are interesting. My case is the following - there is a task on audit and it can be executed in the background, whether it will be executed or not is not interesting.

koenbeuk commented 1 year ago

As mentioned, that is perfectly possible with this library. Just keep in mind that whatever you execute in the background should be considered outside the scope of the request that caused the trigger to fire. So do not rely on services from DI as the request might be finished and those services might get disposed at any moment.

How to run background tasks is outside the scope of this project. I would recommend using Hangfire or a hosted service, however if simply _ = Task.Run(...) is sufficient for your use case then by all means.