When developing microservices, the question arises of providing information from one service to other services as any events occur. It is important to have a system without binding the sender to the recipients. In this case, the Publisher-Subscriber pattern is used.There are many messaging products on the market that support the Publisher-Subscriber pattern, such as Azure Service Bus, RabbitMQ, or Apache Kafka.Recently, I published two NuGet libraries for quick and convenient setup of event-based communication using Azure Service Bus and RabbitMQ. This short how-to article describes the steps for using the latter.Source
Publisher is a .NET Core application that acts as a sender.Subscriber is a .NET Core application that acts as a recipient.How-to
1. In publisher and subscriber applications, install two NuGet libraries.PM> Install-Package Autofac.Extensions.DependencyInjection
PM> Install-Package EventBus.RabbitMQ.Standard
2. In publisher and subscriber applications, add the configuration to appsettings.json .{
"RabbitMq": {
"BrokerName": "test_broker",
"AutofacScopeName": "test_autofac",
"QueueName": "test_queue",
"RetryCount": "5",
"VirtualHost": "your_virtual_host",
"Username": "your_username",
"Password": "your_password",
"Host": "your_host",
"DispatchConsumersAsync": true
}
}
These settings are available on CoudAMQP . Alternatively, you can use RabbitMQ locally ( Docker image ).3. In publisher and subscriber applications, create a class for the event that will be published and subsequently processed.public class ItemCreatedIntegrationEvent : IntegrationEvent
{
public string Title { get; set; }
public string Description { get; set; }
public ItemCreatedIntegrationEvent(string title, string description)
{
Title = title;
Description = description;
}
}
4. In the subscriber application, create a class for the ItemCreatedIntegrationEvent event handler .public class ItemCreatedIntegrationEventHandler : IIntegrationEventHandler<ItemCreatedIntegrationEvent>
{
public ItemCreatedIntegrationEventHandler()
{
}
public async Task Handle(ItemCreatedIntegrationEvent @event)
{
}
}
5. In publisher and subscriber applications, update Program.cs to add one line.public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
6. In the publisher application, update the ConfigureServices method in Startup.cs .public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
var rabbitMqOptions = Configuration.GetSection("RabbitMq").Get<RabbitMqOptions>();
services.AddRabbitMqConnection(rabbitMqOptions);
services.AddRabbitMqRegistration(rabbitMqOptions);
...
}
}
7. In the subscriber application, create the EventBusExtension extension .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;
}
}
8. In the subscriber application, update the 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.UseAuthorization();
...
}
}
9. For demonstration and simplicity, you can create a controller in the publisher application with the method in which the event is published.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();
}
}
10. Now you can publish ItemCreatedIntegrationEvent. Launch both applications, call the POST Publish method in subscriber and handle the event in subscriber .References