3
votes

I've tried using MassTransit to publish a message to a topic named events in an Azure Service Bus. I have problems configuring MassTransit to use my predefined topic events, instead it creates a new topic named by the namespace/classname for the message type. So I wonder how to specify which topic to use instead of creating a new one.

This is the code I've tested with:

using System;
using System.Threading.Tasks;
using MassTransit;
using MassTransit.AzureServiceBusTransport;
using Microsoft.ServiceBus;

namespace PublisherNameSpace
{
    public class Publisher
    {
        public static async Task PublishMessage()
        {
            var topic = "events";
            var bus = Bus.Factory.CreateUsingAzureServiceBus(
                cfg =>
                {
                    var azureServiceBusHost = cfg.Host(new Uri("sb://<busname>.servicebus.windows.net"), host =>
                    {
                        host.OperationTimeout = TimeSpan.FromSeconds(5);
                        host.TokenProvider =
                            TokenProvider.CreateSharedAccessSignatureTokenProvider(
                                "RootManageSharedAccessKey",
                                "<key>"
                            );
                    });

                    cfg.ReceiveEndpoint(azureServiceBusHost, topic, e =>
                    {
                        e.Consumer<TestConsumer>();
                    });
                });

            await bus.Publish<TestConsumer>(new TestMessage { TestString = "testing" });
        }
    }

    public class TestConsumer : IConsumer<TestMessage>
    {
        public Task Consume(ConsumeContext<TestMessage> context)
        {
            return Console.Out.WriteAsync("Consuming message");
        }
    }

    public class TestMessage
    {
        public string TestString { get; set; }
    }
}
3

3 Answers

2
votes

If you want to consume from a specific topic, create a subscription endpoint instead of a receive endpoint, and specify the topic and subscription name in the configuration.

The simplest form is shown in the unit tests:

https://github.com/MassTransit/MassTransit/blob/develop/tests/MassTransit.Azure.ServiceBus.Core.Tests/Subscription_Specs.cs

9
votes

The accepted answer clears up the subscription side:

cfg.SubscriptionEndpoint(
    host,
    "sub-1",
    "my-topic-1",
    e =>
    {
        e.ConfigureConsumer<TestConsumer>(provider);
    });

For those wondering how to get the bus configuration right on the publish side, it should look like:

cfg.Message<TestMessage>(x =>
{
    x.SetEntityName("my-topic-1");
});

You can then call publish on the bus:

await bus.Publish<TestMessage>(message);

Thanks to @ChrisPatterson for pointing this out to me!

0
votes

I was able to send to an Azure Service Bus Topic using the _sendEndpointProvider.GetSendEndpoint(new Uri("topic:shape")); where... "shape" is the topic name.

public class MassTransitController : ControllerBase
{
    private readonly ILogger<MassTransitController> _logger;
    private readonly ISendEndpointProvider _sendEndpointProvider;

    public MassTransitController(ILogger<MassTransitController> logger, ISendEndpointProvider sendEndpointProvider)
    {
        _logger = logger;
        _sendEndpointProvider = sendEndpointProvider;
    }

    [HttpGet]
    public async Task<IActionResult> Get()
    {
        try
        {
            var randomType = new Random();
            var randomColor = new Random();

            var shape = new Shape();
            shape.ShapeId = Guid.NewGuid();
            shape.Color = ShapeType.ShapeColors[randomColor.Next(ShapeType.ShapeColors.Count)];
            shape.Type = ShapeType.ShapeTypes[randomType.Next(ShapeType.ShapeTypes.Count)];

            var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri("topic:shape"));
            await endpoint.Send(shape);

            return Ok(shape);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

I also was able to get a .NET 5 Worker Consumer working with code like this... where the subscription "sub-all" would catch all shapes.. I'm going to make a blog post / git repo of this.

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                services.AddMassTransit(x =>
                {
                    x.UsingAzureServiceBus((context, cfg) =>
                    {
                        cfg.Host("Endpoint=sb://******");

                        cfg.SubscriptionEndpoint(
                            "sub-all",
                            "shape",
                            e =>
                        {
                            e.Handler<Shape>(async context =>
                            {
                                await Console.Out.WriteLineAsync($"Shape Received: {context.Message.Type}");
                            });

                            e.MaxDeliveryCount = 15;
                        });
                    });
                });

                services.AddMassTransitHostedService();
            });