Local Event Bus

The Local Event Bus allows services to publish and subscribe to in-process events. That means it is suitable if two services (publisher and subscriber) are running in the same process.

Publishing Events

There are two ways of publishing local events explained in the following sections.

ILocalEventBus

ILocalEventBus can be injected and used to publish a local event.

Example: Publish a local event when the stock count of a product changes

  1. using System;
  2. using System.Threading.Tasks;
  3. using Volo.Abp.DependencyInjection;
  4. using Volo.Abp.EventBus.Local;
  5. namespace AbpDemo
  6. {
  7. public class MyService : ITransientDependency
  8. {
  9. private readonly ILocalEventBus _localEventBus;
  10. public MyService(ILocalEventBus localEventBus)
  11. {
  12. _localEventBus = localEventBus;
  13. }
  14. public virtual async Task ChangeStockCountAsync(Guid productId, int newCount)
  15. {
  16. //TODO: IMPLEMENT YOUR LOGIC...
  17. //PUBLISH THE EVENT
  18. await _localEventBus.PublishAsync(
  19. new StockCountChangedEvent
  20. {
  21. ProductId = productId,
  22. NewCount = newCount
  23. }
  24. );
  25. }
  26. }
  27. }

PublishAsync method gets a single parameter: the event object, which is responsible to hold the data related to the event. It is a simple plain class:

  1. using System;
  2. namespace AbpDemo
  3. {
  4. public class StockCountChangedEvent
  5. {
  6. public Guid ProductId { get; set; }
  7. public int NewCount { get; set; }
  8. }
  9. }

Even if you don’t need to transfer any data, you need to create a class (which is an empty class in this case).

Inside Entity / Aggregate Root Classes

Entities can not inject services via dependency injection, but it is very common to publish local events inside entity / aggregate root classes.

Example: Publish a local event inside an aggregate root method

  1. using System;
  2. using Volo.Abp.Domain.Entities;
  3. namespace AbpDemo
  4. {
  5. public class Product : AggregateRoot<Guid>
  6. {
  7. public string Name { get; set; }
  8. public int StockCount { get; private set; }
  9. private Product() { }
  10. public Product(Guid id, string name)
  11. : base(id)
  12. {
  13. Name = name;
  14. }
  15. public void ChangeStockCount(int newCount)
  16. {
  17. StockCount = newCount;
  18. //ADD an EVENT TO BE PUBLISHED
  19. AddLocalEvent(
  20. new StockCountChangedEvent
  21. {
  22. ProductId = Id,
  23. NewCount = newCount
  24. }
  25. );
  26. }
  27. }
  28. }

AggregateRoot class defines the AddLocalEvent to add a new local event, that is published when the aggregate root object is saved (created, updated or deleted) into the database.

If an entity publishes such an event, it is a good practice to change the related properties in a controlled manner, just like the example above - StockCount can only be changed by the ChangeStockCount method which guarantees publishing the event.

IGeneratesDomainEvents Interface

Actually, adding local events are not unique to the AggregateRoot class. You can implement IGeneratesDomainEvents for any entity class. But, AggregateRoot implements it by default and makes it easy for you.

It is not suggested to implement this interface for entities those are not aggregate roots, since it may not work for some database providers for such entities. It works for EF Core, but not works for MongoDB for example.

How It Was Implemented?

Calling the AddLocalEvent doesn’t immediately publish the event. The event is published when you save changes to the database;

  • For EF Core, it is published on DbContext.SaveChanges.
  • For MongoDB, it is published when you call repository’s InsertAsync, UpdateAsync or DeleteAsync methods (since MongoDB has not a change tracking system).

Subscribing to Events

A service can implement the ILocalEventHandler<TEvent> to handle the event.

Example: Handle the StockCountChangedEvent defined above

  1. using System.Threading.Tasks;
  2. using Volo.Abp.DependencyInjection;
  3. using Volo.Abp.EventBus;
  4. namespace AbpDemo
  5. {
  6. public class MyHandler
  7. : ILocalEventHandler<StockCountChangedEvent>,
  8. ITransientDependency
  9. {
  10. public async Task HandleEventAsync(StockCountChangedEvent eventData)
  11. {
  12. //TODO: your code that does somthing on the event
  13. }
  14. }
  15. }

That’s all. MyHandler is automatically discovered by the ABP Framework and HandleEventAsync is called whenever a StockCountChangedEvent occurs. You can inject any service and perform any required logic here.

  • Zero or more handlers can subscribe to the same event.
  • A single event handler class can subscribe to multiple events but implementing the ILocalEventHandler<TEvent> interface for each event type.

If you perform database operations and use the repositories inside the event handler, you may need to create a unit of work, because some repository methods need to work inside an active unit of work. Make the handle method virtual and add a [UnitOfWork] attribute for the method, or manually use the IUnitOfWorkManager to create a unit of work scope.

The handler class must be registered to the dependency injection (DI). The sample above uses the ITransientDependency to accomplish it. See the DI document for more options.

Transaction & Exception Behavior

When an event published, subscribed event handlers are immediately executed. So;

  • If a handler throws an exception, it effects the code that published the event. That means it gets the exception on the PublishAsync call. So, use try-catch yourself in the event handler if you want to hide the error.
  • If the event publishing code is being executed inside a Unit Of Work scope, the event handlers also covered by the unit of work. That means if your UOW is transactional and a handler throws an exception, the transaction is rolled back.

Pre-Built Events

It is very common to publish events on entity create, update and delete operations. ABP Framework automatically publish these events for all entities. You can just subscribe to the related event.

Example: Subscribe to an event that published when a user was created

  1. using System.Threading.Tasks;
  2. using Microsoft.AspNetCore.Identity;
  3. using Volo.Abp.DependencyInjection;
  4. using Volo.Abp.Domain.Entities.Events;
  5. using Volo.Abp.EventBus;
  6. namespace AbpDemo
  7. {
  8. public class MyHandler
  9. : ILocalEventHandler<EntityCreatedEventData<IdentityUser>>,
  10. ITransientDependency
  11. {
  12. public async Task HandleEventAsync(
  13. EntityCreatedEventData<IdentityUser> eventData)
  14. {
  15. var userName = eventData.Entity.UserName;
  16. var email = eventData.Entity.Email;
  17. //...
  18. }
  19. }
  20. }

This class subscribes to the EntityCreatedEventData<IdentityUser>, which is published just after a user was created. You may want to send a “Welcome” email to the new user.

There are two types of these events: events with past tense and events with continuous tense.

Events with Past Tense

Events with past tense are published when the related unit of work completed and the entity change successfully saved to the database. If you throw an exception on these event handlers, it can not rollback the transaction since it was already committed.

The event types are;

  • EntityCreatedEventData<T> is published just after an entity was successfully created.
  • EntityUpdatedEventData<T> is published just after an entity was successfully updated.
  • EntityDeletedEventData<T> is published just after an entity was successfully deleted.
  • EntityChangedEventData<T> is published just after an entity was successfully created, updated or deleted. It can be a shortcut if you need to listen any type of change - instead of subscribing to the individual events.

Events with Continuous Tense

Events with continuous tense are published before completing the transaction (if database transaction is supported by the database provider being used). If you throw an exception on these event handlers, it can rollback the transaction since it is not completed yet and the change is not saved to the database.

The event types are;

  • EntityCreatingEventData<T> is published just before saving a new entity to the database.
  • EntityUpdatingEventData<T> is published just before an existing entity is being updated.
  • EntityDeletingEventData<T> is published just before an entity is being deleted.
  • EntityChangingEventData<T> is published just before an entity is being created, updated or deleted. It can be a shortcut if you need to listen any type of change - instead of subscribing to the individual events.

How It Was Implemented?

Pre-build events are published when you save changes to the database;

  • For EF Core, they are published on DbContext.SaveChanges.
  • For MongoDB, they are published when you call repository’s InsertAsync, UpdateAsync or DeleteAsync methods (since MongoDB has not a change tracking system).