سؤال

We are using MassTransit/RabbitMQ and Unity. I've noticed that each consumer is receiving a single message twice. I have looked at other posts where other's are describing this issue as well. I'm clear as to what, specifically is the cause. However all fingers seem to be pointed to MT's SubScriptionBusConfigurator.LoadFrom(IUnityContainer container) method.

I am registering several consumers with my Unity container like so:

 container.RegisterType<ConsumerA>();
 container.RegisterType<ConsumerB>();
 container.RegisterType<ConsumerC>();   

My consumers are all listening for the same message:

public class ConsumerA : Consumes<SomeMessage>.All
{
  public void Consume(SomeMessage message)
  {
     ...
  }
}

My service bus registration looks like:

container.RegisterType<IServiceBus>(new ContainerControlledLifetimeManager(), new InjectionFactory(x => ServiceBusFactory.New(sbc =>
            {
                sbc.ReceiveFrom("rabbitmq://localhost/service_bus");
                sbc.UseRabbitMq();
                sbc.UseNLog();
                sbc.Subscribe(s => s.LoadFrom(container));
                sbc.Subscribe(s => s.Handler<IFault>((context, message) =>
                  _logger.Error(string.Format("ServiceBus Fault: {0}", message.FaultType))));
            })));

After some looking around it seems that my subscription is the problem:

sbc.Subscribe(s => s.LoadFrom(container));

So what is the correct/preferred way to register my consumers so that messages are not sent to each consumer twice??

Thanks!!!

هل كانت مفيدة؟

المحلول

The container specific LoadFrom makes a lot of assumptions. Some of them might be incorrect for you.

I'd suggest changing the calls to sbc.Subscribe(s => container.Resolve<ConsumerA>()), etc. LoadFrom is a convenience method and if it's no longer convenient for whatever reason, just skip it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top