A .NET Core ultra lightweight in-memory event bus implementation.
.NET Core event bus implementation supporting:
> Install-Package Microsoft.Extensions.DependencyInjection
> Install-Package JKang.EventBus
public class MyEvent
{
public string Message { get; set; }
}
class MyEventHandler : IEventHandler<MyEvent>
{
public Task HandleEventAsync(MyEvent @event)
{
Console.WriteLine($"Received message '{@event.Message}'");
return Task.CompletedTask;
}
}
static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddEventBus(builder =>
{
builder.AddInMemoryEventBus(subscriber =>
{
subscriber.Subscribe<MyEvent, MyEventHandler>();
//subscriber.SubscribeAllHandledEvents<MyEventHandler>(); // other way
});
});
}
IServiceProvider serviceProvider = services.BuildServiceProvider();
using (IServiceScope scope = serviceProvider.CreateScope())
{
IEventPublisher eventPublisher = scope.ServiceProvider.GetRequiredService<IEventPublisher>();
eventPublisher.PublishEventAsync(new MyEvent { Message = "Hello, event bus!" }).Wait();
}
Install NuGet package JKang.EventBus.AmazonSns
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");
});
[AmazonSnsTopic("my-event")]
public class MyEvent { }
Install NuGet package JKang.EventBus.AmazonSqs
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";
});
});
[AmazonSqsQueue("https://sqs.ap-southeast-2.amazonaws.com/189107071895/youtube-demo")]
public class MyEvent { }
Any contributions or comments are welcome!