Correct way to publish and subscribe to an explicit service bus topic with MassTransit?

隐身守侯 提交于 2019-12-11 15:33:05

问题


In scenarios where you want to share a Azure Service Bus namespace (to save cost etc), it's helpful to be able to explicitly set queue and topic names rather than rely on MassTransit conventions. Whilst this is straightforward with queues, we've run into difficulty with topics.

The MassTransit documentation is sparse in this area.

It's currently recommending to use a ReceiveEndpoint which appears to forward messages from a topic subscription onto a queue and then subscribe to that queue. We have got this setup working but it seems wasteful when we could just subscribe to the topic directly?

cfg.ReceiveEndpoint(
    host, 
    "my-queue-1",
    e =>
    {
        e.Subscribe("my-topic-1", "sub-1");
        e.ConfigureConsumer<MyConsumer>(provider);
        e.MaxConcurrentCalls = 24;
    });

I've discovered SubscriptionEndpoint which appears to do just this. We'd like to subscribe to a specifically named topic 'my-topic-1' with a named subscription 'sub-1'. We can replace the ReceiveEndpoint with a SubscriptionEndpoint as follows:

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

We'd like the publisher to be in a separate application, what should its bus config and publish code look like? The docs hint that the Publish Topology can be used to supply an explicit Service Bus topic name. We've tried setting the TopicDescription.Path as follows:

Bus.Factory.CreateUsingAzureServiceBus(..., cfg =>
{
    cfg.Publish<IMyMessage>(x =>
    {
        x.TopicDescription.Path = "my-topic-1";
    });
});
await bus.Publish<IMyMessage>(message);

But the message is not getting through to the consumer.

It seems this is a issue with publishing. Although the publisher creates the topic 'my-topic-1' it still publishes the message to the topic 'my.namespace/imymessage'. Looking at the source of ServiceBusMessagePublishTopology I can see why this is the case (_topicDescription vs _topicConfigurator) but it doesn't seem obvious how to override the topic's Path with the topic path hardcoded to the messageTopology's EntityName (#L42) and the then only a setter on BasePath available.

来源:https://stackoverflow.com/questions/57593881/correct-way-to-publish-and-subscribe-to-an-explicit-service-bus-topic-with-masst

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