antonyvorontsov / RabbitMQ.Client.Core.DependencyInjection

.Net Core library-wrapper of RabbitMQ.Client for Dependency Injection.
MIT License
111 stars 36 forks source link

How can I use multiple Exchange #4

Closed MohammadAkbari closed 4 years ago

MohammadAkbari commented 4 years ago

Hi Thanks for this library.

I need to consume from multiple Exchange in my project, How can do this? do this library support these features? How config in appSettings file or in the dependency Injection

antonyvorontsov commented 4 years ago

Hi Mohammad!

Despite you have already closed this issue and managed it yourself (as I assume) I will write an answer for those people who will have the same question.

You can configure multiple exchanges using multiple calls of AddProductionExchange or AddConsumptionExchange methods passing different configuration sections from appsettings.json or configuring it manually with RabbitMqExchangeOptions.

Looks like this:

services.AddRabbitMqClient(rabbitMqSection)
    .AddConsumptionExchange("consumption.exchange", Configuration.GetSection("ConsumptionExchange"))
    .AddConsumptionExchange("another.consumption.exchange", Configuration.GetSection("AnotherConsumptionExchange"))
        .AddProductionExchange("production.exchange", Configuration.GetSection("ProductionExchange"));
// Handlers configuring goes after.

And your appsettings.json file can be like this:

{
  "RabbitMq": {
    "HostName": "127.0.0.1",
    "Port": "5672",
    "UserName": "guest",
    "Password": "guest"
  },
  "ConsumptionExchange": {
    "Queues": [
      {
        "Name": "firstQueue",
        "RoutingKeys": [ "firstQueue.key" ]
      }
    ]
  },
  "AnotherConsumptionExchange": {
    "Queues": [
      {
        "Name": "secondQueue",
        "RoutingKeys": [ "secondQueue.key" ]
      }
    ]
  },
  "ProductionExchange": {
    "Queues": [
      {
        "Name": "thirdQueue",
        "RoutingKeys": [ "thirdQueue.key" ]
      }
    ]
  }
}

So this configuration allows you to configure multiple exchanges and bind different queues to them without any intersection.

If something is unclear you can reopen the issue and ask me for some additional help.

Regards, Antony