3

I am trying to get rebus with RabbitMQ to retrieving the messages that I have successfully published to the message queue. What am I not doing that needs to be done. below is the example code?

static async Task MainSubscribeAsync()
        {

            var connection = "amqp://jhgj67546:yjyj5565@localhost";

            using (var activator = new BuiltinHandlerActivator())
            {
                activator.Register(() => new WagonHandler());

                var bus = Configure.With(activator)
                    .Logging(l => l.ColoredConsole())
                    .Transport(t => t.UseRabbitMq(connection, "wagon_v1")
                        .ExchangeNames(directExchangeName: "WamosExchange"))
                    .Start();

                await activator.Bus.Subscribe<Wagon>();

                Console.WriteLine("Done");
            }
        }

The handler class looks like this

class WagonHandler : IHandleMessages
    {
        public async Task Handle(Wagon message)
        {
            Console.WriteLine($"Token {message.Token}");
            Console.WriteLine($"WagonId {message.WagonId}");
        }
    }
Andrew
  • 2,571
  • 2
  • 31
  • 56

1 Answers1

3

This code

static async Task MainSubscribeAsync()
{

    var connection = "amqp://jhgj67546:yjyj5565@localhost";

    using (var activator = new BuiltinHandlerActivator())
    {
        activator.Register(() => new WagonHandler());

        var bus = Configure.With(activator)
            .Logging(l => l.ColoredConsole())
            .Transport(t => t.UseRabbitMq(connection, "wagon_v1")
                .ExchangeNames(directExchangeName: "WamosExchange"))
            .Start();

        await activator.Bus.Subscribe<Wagon>();

        Console.WriteLine("Done");
    }
}

starts the bus, establishes a subscription, and then immediately shuts down because the BuiltinHandlerActivator is disposed.

This means that you could be lucky and maybe receive a few messages, but in most cases you probably won't :)

You should instead configure the bus when your application starts, and then keep it as a singleton instance (as described on the wiki page about Rebus' instance policies) for the entire lifetime of your application, disposing it only when the application is shutting down.

mookid8000
  • 18,258
  • 2
  • 39
  • 63