NetKit.EventDrivenCommunication is a messaging component that enables Pub\Sub communication between system components or services.
Softeq.NetKit.Components.EventBus- exposes core Pub\Sub abstractions and models of the component.Softeq.NetKit.Components.EventBus.Service- Pub\Sub implementation that uses Azure Service Bus as a communication mechanins.Softeq.NetKit.Integrations.EventLog- SQL DB Store for event state.
- Check-out master branch from repository
- Add a reference to Softeq.NetKit.Components.EventBus into target project.
- Add a reference to Softeq.NetKit.Components.EventBus.Service into target project.
- (Optional) Add a reference to Softeq.NetKit.Integrations.EventLog into target project.
- Create custom event class inherited from
IntegrationEventbase class
public class UserCreatedEvent : IntegrationEvent
{
public UserCreatedEvent(Guid userId)
{
UserId = userId;
}
public Guid UserId { get; set; }
}- Implement
IEventHandler<>interface to handle received custom events
public class UserCreatedEventHandler : IEventHandler<UserCreatedEvent>
{
public async Task Handle(UserCreatedEvent @event)
{
//Code goes here
}
}- Set up and register ServiceBus configuration
ServiceBusPersisterConnectionConfigurationin your DI container
builder.Register(x =>
{
var context = x.Resolve<IComponentContext>();
var config = context.Resolve<IConfiguration>();
return new ServiceBusPersisterConnectionConfiguration
{
ConnectionString = config["CONN_STR"],
TopicConfiguration = new ServiceBusPersisterTopicConnectionConfiguration
{
TopicName = config["TOPIC"],
SubscriptionName = config["SUBSCRIPTION"]
},
QueueConfiguration = new ServiceBusPersisterQueueConnectionConfiguration
{
QueueName = config["QUEUE"]
}
};
}).As<ServiceBusPersisterConnectionConfiguration>();- Register
IServiceBusPersisterConnectionimplementation
builder.RegisterType<ServiceBusPersisterConnection>()
.As<IServiceBusPersisterConnection>();- Set up and register Service Bus message configuration
MessageQueueConfiguration
builder.Register(context =>
{
var config = context.Resolve<IConfiguration>();
return new MessageQueueConfiguration
{
TimeToLive = Convert.ToInt32(config["MSG_TTL"])
};
});- Register
IEventBusSubscriptionsManagerimplementation
builder.RegisterType<EventBusSubscriptionsManager>()
.As<IEventBusSubscriptionsManager>();- Register
IEventBusServiceimplementation
builder.RegisterType<EventBusService>()
.As<IEventBusPublisher>();
builder.RegisterType<EventBusService>()
.As<IEventBusSubscriber>();- Register
IEventHandlerimplementations
builder.RegisterType<UserCreatedEventHandler>();- (Optional) Register
IIntegrationEventLogServiceimplementation
builder.RegisterType<IntegrationEventLogService>()
.As<IIntegrationEventLogService>();- Enable the listeners on desired event sources
IEventBusSubscriber eventBus;
eventBus.RegisterQueueListener();
eventBus.RegisterSubscriptionListenerAsync().GetAwaiter().GetResult();- Register your custom event handlers
eventBus.SubscribeAsync<UserCreatedEvent, UserCreatedEventHandler>().GetAwaiter().GetResult();Inject IEventBusPublisher and IIntegrationEventLogService into your service
public class SomeService
{
private readonly IEventBusPublisher _eventPublisher;
private readonly IIntegrationEventLogService _eventLogService;
public SomeService(IEventBusPublisher eventPublisher, IIntegrationEventLogService eventLogService)
{
_eventPublisher = eventPublisher;
_eventLogService = eventLogService;
}
public async Task Do(UserCreatedEvent @event)
{
await _eventLogService.SaveAsync(@event);
await _eventPublisher.PublishToTopicAsync(@event, delayInSeconds);
await _eventLogService.MarkAsPublishedAsync(@event);
}
}This project is maintained by Softeq Development Corp.
We specialize in .NET core applications.
We welcome any contributions.
The Query Utils project is available for free use, as described by the LICENSE (MIT).