Skip to content

Run2948/EventBus

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JKang.EventBus

.NET Core event bus implementation supporting:

  • In-memory event dispatching (publishing and subscription)
  • publishing event to Amazon SNS
  • publishing event to Amazon SQS
  • delayed execution of event handler

NuGet packages

Quick start:

  1. Create a console application with the following NuGet packages installed:
> Install-Package Microsoft.Extensions.DependencyInjection
> Install-Package JKang.EventBus
  1. Create an event class
    public class MyEvent
    {
        public string Message { get; set; }
    }
  1. Optionally implement one or multiple handlers subscribing to the event
    class MyEventHandler : IEventHandler<MyEvent>
    {
        public Task HandleEventAsync(MyEvent @event)
        {
            Console.WriteLine($"Received message '{@event.Message}'");
            return Task.CompletedTask;
        }
    }
  1. Configure and register event bus using Dependency Injection
    static void Main(string[] args)
    {
        IServiceCollection services = new ServiceCollection();

        services.AddEventBus(builder =>
        {
            builder.AddInMemoryEventBus(subscriber =>
            {
                subscriber.Subscribe<MyEvent, MyEventHandler>();
                //subscriber.SubscribeAllHandledEvents<MyEventHandler>(); // other way
            });
        });
    }
  1. Publish an event
    IServiceProvider serviceProvider = services.BuildServiceProvider();
    using IServiceScope scope = serviceProvider.CreateScope();
    IEventPublisher eventPublisher = scope.ServiceProvider.GetRequiredService<IEventPublisher>();
    await eventPublisher.PublishEventAsync(new MyEvent { Message = "Hello, event bus!" }, 3);

Publish event to Amazon Simple Notification Service (SNS)

  1. Install NuGet package JKang.EventBus.AmazonSns

  2. Configure publishing events to AWS SNS

        services.AddEventBus(builder =>
        {
            builder
                //.AddInMemoryEventBus() // uncomment to keep using in-memory event bus in the same time
                .PublishToAmazonSns(x => x.Region = "eu-west-3");
        });
  1. Optionally It's possible to customize AWS SNS topic name using annotation
    [AmazonSnsTopic("my-event")]
    public class MyEvent { }

Publish event to Amazon Simple Queue Service (SQS)

  1. Install NuGet package JKang.EventBus.AmazonSqs

  2. Configure publishing events to AWS SQS

        services.AddEventBus(builder =>
        {
            builder
                //.AddInMemoryEventBus() // uncomment to keep using in-memory event bus in the same time
                .PublishToAmazonSqs(options =>
                    {
                        options.AccessKeyId = "";
                        options.SecretAccessKey = "";
                        options.DefaultQueueUrl = "";
                        options.Region = "us-east-1";
                    });
        });
  1. Optionally It's possible to customize AWS SQS queue url using annotation
    [AmazonSqsQueue("https://sqs.ap-southeast-2.amazonaws.com/189107071895/youtube-demo")]
    public class MyEvent { }

Any contributions or comments are welcome!

About

A .NET Core ultra lightweight in-memory event bus implementation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.4%
  • HTML 1.6%