MassTransit consumer wont trigger

Startup.cs

 static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingAzureServiceBus(h =>
            {
                var host = h.Host(serverAddressURI
                    ,
                    z =>
                    {
                        z.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", key);
                    });

                h.ReceiveEndpoint(host, "orderqueue",
                    ep =>
                    {
                         ep.Consumer<OrderConsumer>(); 
                    });
            });

            bus.Start();
            TaskUtil.Await(() => bus.Publish(new Order {Text = "Hi3"}));        
            bus.Stop();
        }

OrderConsumer

public class OrderConsumer : IConsumer    
    {
        public async Task Consume(ConsumeContext<Order> context)
        {
            await Console.Out.WriteLineAsync($"Shipping order: {context.Message.Text}");

        }
    }

Im tryng to setup a simple test with masstransit, not sure whats the issue, the message is sent the my queue, i can see it at the Azure service bus dashboard at the azure portal. But the consumer never gets triggered, and i there isnt any errors.. Any idea?

Upvotes: 1

Views: 351

Answers (1)

nizmow
nizmow

Reputation: 579

It's possibly because your application is stopping before the consumer gets a chance to complete consuming. Try adding a TaskUtil.Await(() => Task.Delay(2000)) immediately after your publish to give your consumer time to consume before the application exits.

This shouldn't be a problem in a real application, of course.

Off topic, you can use an async Main method to avoid TaskUtil.Await(): public static async Task Main(string[] args).

Upvotes: 1

Related Questions