gpt4 book ai didi

azure - 从 Microsoft.Azure.ServiceBus 迁移到 Azure.Messaging.ServiceBus

转载 作者:行者123 更新时间:2023-12-02 22:59:15 30 4
gpt4 key购买 nike

我正在尝试更新此函数以使用 Azure.Messaging.ServiceBus 并完全删除 Microsoft.Azure.ServiceBus,但似乎找不到任何相关资源。有人知道如何使用这个包向主题发送消息吗?

旧函数是:

  public async Task SendMessageToServiceBusTopic<T>(T request, string topicSubName, string submissionNumber)
{
ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder =
new ServiceBusConnectionStringBuilder(settings.ServiceBusConnectionString)
{
EntityPath = settings.ServiceBusTopic
};


TopicClient topicClient = new TopicClient(serviceBusConnectionStringBuilder);

byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));

await topicClient.SendAsync(new Message(bytes)
{
CorrelationId = context.CorrelationId,
Label=topicSubName,
UserProperties = { new KeyValuePair<string, object>("TrackingId", submissionNumber) }
});
}

到目前为止我已经做到了:

我的方向正确吗?

  public async Task SendMessageToServiceBusTopic<T>(T request, string topicSubName, string submissionNumber)
{
ServiceBusClient client = new ServiceBusClient(settings.ServiceBusConnectionString);
ServiceBusSender s = client.CreateSender(settings.ServiceBusTopic);


byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request));
await s.SendMessageAsync(new ServiceBusMessage(bytes));
}

最佳答案

虽然您每次都可以构建一个服务总线客户端,但这并不理想。假设您使用的是最新的 In-Proc SDK,您可以使用以下选项之一:

[FunctionName("PublishToTopic")]
public static async Task Run(
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
[ServiceBus("<topic_name>", Connection = "<connection_name>")] IAsyncCollector<ServiceBusMessage> collector)
{
await collector.AddAsync(new ServiceBusMessage(new BinaryData($"Message 1 added at: {DateTime.Now}")));
await collector.AddAsync(new ServiceBusMessage(new BinaryData($"Message 2 added at: {DateTime.Now}")));
}

或者,

[FunctionName("PublishWithSender"]
public static async Task Run(
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
[ServiceBus("<topic_name>", Connection = "<connection_name>")] ServiceBusSender sender)
{
await sender.SendMessagesAsync(new[]
{
new ServiceBusMessage(new BinaryData($"Message 1 added at: {DateTime.Now}")),
new ServiceBusMessage(new BinaryData($"Message 2 added at: {DateTime.Now}"))
});
}

对于独立工作人员 SDK,情况有些不同。请参阅this post了解详情。

关于azure - 从 Microsoft.Azure.ServiceBus 迁移到 Azure.Messaging.ServiceBus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69980614/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com