本文介绍了如何指定与MassTransit一起使用的Azure服务总线主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用MassTransit在Azure Service Bus中将消息发布到名为events的主题.我在配置MassTransit以使用我的预定义主题events时遇到问题,相反,它为消息类型创建了一个由名称空间/类名命名的新主题.因此,我想知道如何指定要使用的主题,而不是创建一个新主题.

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.

这是我测试过的代码:

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; }
    }
}

推荐答案

如果要从特定主题使用,请创建订阅终结点而不是接收终结点,并在配置中指定主题和订阅名称.

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

这篇关于如何指定与MassTransit一起使用的Azure服务总线主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 06:23