How to specify which Azure Service Bus Topic to use with MassTransit

浪尽此生 提交于 2020-06-27 16:39:51

问题


I've tried using MassTransit to publish a message to a topic named events in an Azure Service Bus. I have problem 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; }
    }
}

回答1:


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/src/MassTransit.Azure.ServiceBus.Core.Tests/Subscription_Specs.cs




回答2:


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!



来源:https://stackoverflow.com/questions/49434228/how-to-specify-which-azure-service-bus-topic-to-use-with-masstransit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!