Skip to content

Softeq/EventDrivenCommunication

Repository files navigation

NuGet Azure DevOps builds

NetKit.EventDrivenCommunication

NetKit.EventDrivenCommunication is a messaging component that enables Pub\Sub communication between system components or services.

Structure

  1. Softeq.NetKit.Components.EventBus - exposes core Pub\Sub abstractions and models of the component.
  2. Softeq.NetKit.Components.EventBus.Service - Pub\Sub implementation that uses Azure Service Bus as a communication mechanins.
  3. Softeq.NetKit.Integrations.EventLog - SQL DB Store for event state.

Getting Started

Install

  1. Check-out master branch from repository
  2. Add a reference to Softeq.NetKit.Components.EventBus into target project.
  3. Add a reference to Softeq.NetKit.Components.EventBus.Service into target project.
  4. (Optional) Add a reference to Softeq.NetKit.Integrations.EventLog into target project.

Develop

  1. Create custom event class inherited from IntegrationEvent base class
    public class UserCreatedEvent : IntegrationEvent
	{
		public UserCreatedEvent(Guid userId)
		{
		    UserId = userId;
		}

		public Guid UserId { get; set; }
	}
  1. Implement IEventHandler<> interface to handle received custom events
    public class UserCreatedEventHandler : IEventHandler<UserCreatedEvent>
    {
	    public async Task Handle(UserCreatedEvent @event)
	    {
		    //Code goes here
	    }
    }

Configure DI Container

  1. Set up and register ServiceBus configuration ServiceBusPersisterConnectionConfiguration in 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>();
  1. Register IServiceBusPersisterConnection implementation
    builder.RegisterType<ServiceBusPersisterConnection>()
        .As<IServiceBusPersisterConnection>();
  1. 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"])
        };
    });
  1. Register IEventBusSubscriptionsManager implementation
    builder.RegisterType<EventBusSubscriptionsManager>()
                .As<IEventBusSubscriptionsManager>();
  1. Register IEventBusService implementation
    builder.RegisterType<EventBusService>()
                .As<IEventBusPublisher>();
    builder.RegisterType<EventBusService>()
                .As<IEventBusSubscriber>();
  1. Register IEventHandler implementations
    builder.RegisterType<UserCreatedEventHandler>();
  1. (Optional) Register IIntegrationEventLogService implementation
    builder.RegisterType<IntegrationEventLogService>()
                .As<IIntegrationEventLogService>();

Configure service

  1. Enable the listeners on desired event sources
    IEventBusSubscriber eventBus;
    eventBus.RegisterQueueListener();
    eventBus.RegisterSubscriptionListenerAsync().GetAwaiter().GetResult();
  1. Register your custom event handlers
    eventBus.SubscribeAsync<UserCreatedEvent, UserCreatedEventHandler>().GetAwaiter().GetResult();

Use

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);
        }
    }

About

This project is maintained by Softeq Development Corp.

We specialize in .NET core applications.

Contributing

We welcome any contributions.

License

The Query Utils project is available for free use, as described by the LICENSE (MIT).

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages