4
votes

I am in the process of converting an application from using MT3 with RabbitMQ into using Azure Service Bus and MassTransit isn't configuring the queue the same way in Azure as it does with RMQ, and with the documentation being on the light side (here) I wanted to see if anyone else has solved this

I am using MT 3.4 and Microsoft.ServiceBus 3.0 for the TokenProvider, I have a utility class which creates the bus (following this example):

    public static IBus CreateBus()
    {
        var busControl = Bus.Factory.CreateUsingAzureServiceBus(sbc =>
        {
            var host = sbc.Host(new Uri("sb://<sbname>.servicebus.windows.net/"), h =>
            {
                h.OperationTimeout = TimeSpan.FromSeconds(5);
                h.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("<KeyName>", "<Key>");
            });

            sbc.ReceiveEndpoint(host, "command_queue", ep =>
            {
                ep.SubscribeMessageTopics = true;
                ep.UseRetry(Retry.Incremental(5, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5)));
            });
        });

        return busControl;
    }

I have a Web API controller which uses the bus to publish commands to the queue:

[RoutePrefix("customer")]
public class CustomerController : ApiController
{
    private readonly IBus _serviceBus;

    public CustomerController()
    {
        _serviceBus = AzureServiceBusUtils.CreateBus();
    }

    [HttpPost, Route("register")]
    public async Task<HttpResponseMessage> Register()
    {
        var command = JsonConvert.DeserializeObject<RegisterNewCustomerCommand>(Encoding.ASCII.GetString(Request.Content.ReadAsByteArrayAsync().Result));
        await _serviceBus.Publish(command);
        return Request.CreateResponse(HttpStatusCode.OK);
    }
}

The RegisterNewCustomerCommand is just a simple Name, Address, etc. C# class:

namespace AZSB.Commands
{
    public class RegisterNewCustomerCommand
    {
        public string Name { get; }
        ...
        public RegisterNewCustomerCommand(...) {...}
    }
}

Now when I fire a message through, a topic is created on Azure (AZSB.Commands/RegisterNewCustomerCommand) but this isn't linked as I would expect to the command_queue, and to add to it, when I click on the topic within the Azure portal, the details panel just hangs (so i can't manually configure it to a manually created queue)

Am I missing something? Alastair

1

1 Answers

4
votes

You need to add a message consumer for that type in the receive endpoint. The code above has no consumer, so there are no types bound.

class YourConsumer :
    IConsumer<RegisterNewCustomerCommand>
{}

cfg.ReceiveEndpoint("your_queue", x => 
{
    x.Consumer<YourConsumer>();
});

Then you'll have a consumer that accepts that message type, which will cause MassTransit to bind the topic to the queue.