EventBus.RabbitMQ.Standard

A library for the event-bus communication by using RabbitMQ


Keywords
dotnet, netstandard, csharp, rabbitmq, eventbus, communication
License
MIT
Install
Install-Package EventBus.RabbitMQ.Standard -Version 3.2.0-merge-with-base-library.2

Documentation

EventBus.RabbitMQ.Standard

Actions Status NuGet Badge CodeFactor

A library for the event-based communication by using RabbitMQ.

Samples

How-To

Install a NuGet package.

PM> Install-Package EventBus.RabbitMQ.Standard

Add configuration to appsettings.json.

{
  "RabbitMq": {
    "BrokerName": "test_broker",
    "QueueName": "test_queue",
    "RetryCount": "5",
    "VirtualHost": "your_virtual_host",
    "Username": "your_username",
    "Password": "your_password",
    "Host": "your_host",
    "DispatchConsumersAsync": true
  }
}

Note: I find pretty easy to use CloudAMQP. Alternatively, you can run a Docker container for RabbiMQ on a local machine.

In publisher and subscriber apps, create a new class called ItemCreatedIntegrationEvent.

public class ItemCreatedIntegrationEvent : IntegrationEvent
{
    public string Title { get; set; }
    public string Description { get; set; }

    public ItemCreatedIntegrationEvent(string title, string description)
    {
        Title = title;
        Description = description;
    }
}

In the subscriber app, create a new class called ItemCreatedIntegrationEventHandler.

public class ItemCreatedIntegrationEventHandler : IIntegrationEventHandler<ItemCreatedIntegrationEvent>
{
    public ItemCreatedIntegrationEventHandler()
    {
    }

    public async Task Handle(ItemCreatedIntegrationEvent @event)
    {
        //Handle the ItemCreatedIntegrationEvent event here.
    }
}

In the publisher app, modify the method ConfigureServices in Startup.cs.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        ...

        var rabbitMqOptions = Configuration.GetSection("RabbitMq").Get<RabbitMqOptions>();

        services.AddRabbitMqConnection(rabbitMqOptions);
        services.AddRabbitMqRegistration(rabbitMqOptions);

        ...
    }
}

In the subscriber app, create an extension called EventBusExtension.

public static class EventBusExtension
{
    public static IEnumerable<IIntegrationEventHandler> GetHandlers()
    {
        return new List<IIntegrationEventHandler>
        {
            new ItemCreatedIntegrationEventHandler()
        };
    }

    public static IApplicationBuilder SubscribeToEvents(this IApplicationBuilder app)
    {
        var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();

        eventBus.Subscribe<ItemCreatedIntegrationEvent, ItemCreatedIntegrationEventHandler>();

        return app;
    }
}

In the subscriber app, modify ConfigureServices and Configure methods in Startup.cs.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        ...

        var rabbitMqOptions = Configuration.GetSection("RabbitMq").Get<RabbitMqOptions>();

        services.AddRabbitMqConnection(rabbitMqOptions);
        services.AddRabbitMqRegistration(rabbitMqOptions);
        services.AddEventBusHandling(EventBusExtension.GetHandlers());

        ...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...

        app.SubscribeToEvents();

        ...
    }
}

Publish the ItemCreatedIntegrationEvent event in the publisher app by using the following code, for example in a controller.

public class ItemController : ControllerBase
{
    private readonly IEventBus _eventBus;

    public ItemController(IEventBus eventBus)
    {
        _eventBus = eventBus;
    }

    [HttpPost]
    public IActionResult Publish()
    {
        var message = new ItemCreatedIntegrationEvent("Item title", "Item description");

        _eventBus.Publish(message);

        return Ok();
    }
}

Code of Conduct

See CODE_OF_CONDUCT.md.

Contributing

See CONTRIBUTING.md.

References