Defining an event bus interface Let’s start with some implementation code for the event bus interface and possible implementations
for exploration purposes. The interface should be generic and straightforward, as in the following
interface.
public interface IEventBus
{
void
Publish
(IntegrationEvent @event);
void
Subscribe()
where T : IntegrationEvent
where TH : IIntegrationEventHandler;
void
SubscribeDynamic
(
string
eventName)
where TH : IDynamicIntegrationEventHandler;
138
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
void
UnsubscribeDynamic
(
string
eventName)
where TH : IDynamicIntegrationEventHandler;
void
Unsubscribe()
where TH : IIntegrationEventHandler where T : IntegrationEvent;
}
The
Publish
method is straightforward. The event bus will broadcast the integration event passed to
it to any microservice, or even an external application, subscribed to that event. This method is used
by the microservice that is publishing the event.
The
Subscribe
methods (you can have several implementations depending on the arguments) are
used by the microservices that want to receive events. This method has two arguments. The first is the
integration event to subscribe to (
IntegrationEvent
). The second argument is the integration event
handler (or callback method), named
IIntegrationEventHandler , to be executed when the
receiver microservice gets that integration event message.
Additional resources Some production-ready messaging solutions:
•
Azure Service Bus https://learn.microsoft.com/azure/service-bus-messaging/