MassTransit and IOptions injection of Consumer by binding

41 Views Asked by At

Is it possible to use the same consumer but distinguished by options? I tried in this way but the options are always the same for all consumers.

I defined the options here

  services.Configure<ProdConsOptions>(x => {x.Name ="hello";});

added a consumer

  services.AddMassTransit(x =>{
   x.AddConsumer<ProdConsumer>(c =>
   {
       c.Options<ProdConsOptions>(c => c.Name = "test");
   });
   x.UsingRabbitMq((context, cfg) =>
   {
       cfg.Host("localhost", "/", h =>
       {
           h.Username("...");
           h.Password("...");
       });

       cfg.ConfigureMessageTopology();


       cfg.ReceiveEndpoint($"cons1.ns.prod", e =>
       {
           e.ConfigureConsumeTopology = false;
           e.ConfigureConsumer<ProdConsumer>(context, c=>
           {
               c.Options<ProdConsOptions>(c => c.Name = "Create");
           });

           e.Bind("ns.prod", s =>
           {
               s.RoutingKey="#.create";
               s.Durable = true;
               s.ExchangeType = RabbitMQ.Client.ExchangeType.Topic;
           });

       });


       cfg.ReceiveEndpoint($"cons2.ns.prod", e =>
       {
           
           e.ConfigureConsumeTopology = false;
           e.ConfigureConsumer<ProdConsumer>(context, c =>{
               c.Options<ProdConsOptions>(c => c.Name = "Update");
           });


           e.Bind("ns.prod", s =>
           {
               s.RoutingKey="#.update";
               s.Durable = true;
               s.ExchangeType = RabbitMQ.Client.ExchangeType.Topic;
           });

       });

with the consumer so defined:

       public class ProdConsumer : IConsumer<Prod>
       {
          private readonly IOptionsMonitor<ProdConsOptions> _option;

          public UpdateConsumer(IOptionsMonitor<ProdConsOptions> option){
             _option = option;
           }

          public Task Consume(ConsumeContext<Update> context)
          {
             Console.WriteLine(_option?.CurrentValue.Name??"");
             Console.WriteLine(context.Message.ToString());
             return Task.CompletedTask;
          }
       }

Well the option current value has always the Name ="hello" as defined on the first line.

0

There are 0 best solutions below