2
votes

I have an issue with MassTransit not sending messages with the following code - this is a port from our Azure Service Bus code which works fine. The examples in GitHub populate the Queue - Starbucks example, so my infrastructure is working.

Can anyone please suggest why this is not sending messages? I have created both the queue and exchange, and tried without.

The console app prints out the expected results.

Thanks in advance.

public class Program
    {
        static void Main()
        {
            IBusControl busControl = CreateBus();

            TaskUtil.Await(() => busControl.StartAsync());

            List<Task> tList = new List<Task>();
            for (int i = 0; i < 10; i++)
            {
                var t = Send(busControl);
                tList.Add(t);
            }
            Task.WaitAll(tList.ToArray());
            Console.WriteLine("done!");
        }

        private static async Task Send(IBusControl busControl)
        {
            var endpoint = await busControl.GetSendEndpoint(new Uri("rabbitmq://localhost/test"));

            Console.WriteLine("Sending");

            await endpoint.Send(new SomethingHappenedMessage()
            {
                What = "Stuff",
                When = DateTime.Now
            });

            Console.WriteLine("Sent");
        }

        static IBusControl CreateBus()
        {
            return Bus.Factory.CreateUsingRabbitMq(x => x.Host(new Uri("rabbitmq://localhost"), h =>
            {
                h.Username("guest");
                h.Password("guest");
            }));
        }
}

 public interface SomethingHappened
    {
        string What { get; }
        DateTime When { get; }
    }

    public class SomethingHappenedMessage : SomethingHappened
    {
        public string What { get; set; }
        public DateTime When { get; set; }
    }
1
Is the test exchange bound to the test queue? - Chris Patterson
I would add stopping the bus at the end to ensure all open pipelines finish executing. - Alexey Zimarev
Chris, binding the queue worked, thank you - I am new to RabbitMQ. This doesn't explain why the Starbucks sample worked out of the box though - I think this is MassTransit 2.0 though. I'll mark it correct if you answer. Thanks again. - KnowHoper
The Starbucks sample adds consumers, which creates the queue bindings - your sender above doesn't create a receive endpoint - so no consumers. - Chris Patterson
First of all, this is for Send, not Publish - and it should be your last choice, to be honest. Start your receiveEndpoints before your producers come online. It isn't that complicated. Don't try to make it hard. - Chris Patterson

1 Answers

6
votes

When you send messages with MassTransit using RabbitMQ, by default the bindings for the queue are not created. It is assumed that a receive endpoint in a service will create the queue and related bindings.

To ensure that the queue and bindings exist when sending a message, you can modify the endpoint address to include some additional query string parameters as shown below:

rabbitmq://localhost/vhost/exchange_name?bind=true&queue=queue_name

In the case of a receive endpoint, the exchange name and queue name are the same.