如何在发布时使用 MassTransit 设置 Azure Service Bus 主题?

huangapple go评论60阅读模式
英文:

How to set the Azure Service Bus topic at publishing time with MassTransit?

问题

我正在尝试在发布时设置主题,因为我需要从消息内容中派生它。

我尝试过在发布上下文中工作,就像这样(在一个IPipe中):

public Task Send(PublishContext<MyMessageType> context)
{
    context.DestinationAddress = CalculateDestinationAddress(context);

    return Task.CompletedTask;
}

但没有成功。我还尝试寻找其他覆盖主题的方法/时机,但没有找到任何信息。

有人知道如何完成这个任务吗?

英文:

I'm trying to set the topic at publishing time, because I need to derive it from the message content.

I tried to work on the publishing context, like (in a IPipe):

public Task Send(PublishContext&lt;MyMessageType&gt; context)
{
    context.DestinationAddress = CalculateDestinationAddress(context);

    return Task.CompletedTask;
}

But didn't work. Also looked for other ways/points to override the topic but didn't find anything.

Anyone aware of how to accomplish this?

答案1

得分: 0

如果您想要发送到任何给定名称的主题,可以使用_topic_地址语法如下:

var endpoint = await sendEndpointProvider.GetSendEndpoint(new Uri("topic:name"));
await endpoint.Send(message);

sendEndpointProvider 可以通过在消费消息时使用 ConsumeContext 获得,或者通过容器中的 ISendEndpointProvider 接口获得。

英文:

If you want to send to a topic of any given name, using the topic address syntax instead:

var endpoint = await sendEndpointProvider.GetSendEndpoint(new Uri(&quot;topic:name&quot;));
await endpoint.Send(message);

sendEndpointProvider is available via the ConsumeContext when consuming a message, or via the ISendEndpointProvider interface from the container.

huangapple
  • 本文由 发表于 2023年6月29日 15:36:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定