Search code examples
asp.net-coreunit-of-workabp-framework

How do I publish a custom event after the current UoW transaction is successfully completed?


There are predefined events like EntityCreatedEventData that are published after the transaction is successfully committed.

I would like to publish a custom Event after the transaction is successfully committed. Is that possible with the current API?


Solution

  • Yes, that is possible with the current API.

    From https://docs.abp.io/en/abp/latest/Unit-Of-Work#other-iunitofwork-properties-methods:

    • OnCompleted method gets a callback action which is called when the unit of work successfully completed (where you can be sure that all changes are saved).
    var uow = _unitOfWorkManager.Current;
    uow.OnCompleted(() =>
    {
        await _eventBus.PublishAsync(new MyCustomEvent());
    });
    

    This is the same API that is used for EntityCreatedEventData.