azure - .net 5 Azure 总线服务队列侦听器
问题描述
.net 5 的 SDK 已更改,并且包 Microsoft.Azure.ServiceBus 将被弃用,因此 IQueueClient 不再存在。
创建 SB Queue 侦听器的最新解决方案是什么?
解决方案
使用服务总线处理器
string connectionString = "<connection_string>";
string queueName = "<queue_name>";
// since ServiceBusClient implements IAsyncDisposable we create it with "await using"
await using var client = new ServiceBusClient(connectionString);
var options = new ServiceBusProcessorOptions
{
// By default or when AutoCompleteMessages is set to true, the processor will complete the message after executing the message handler
// Set AutoCompleteMessages to false to [settle messages](https://docs.microsoft.com/en-us/azure/service-bus-messaging/message-transfers-locks-settlement#peeklock) on your own.
// In both cases, if the message handler throws an exception without settling the message, the processor will abandon the message.
AutoCompleteMessages = false,
// I can also allow for multi-threading
MaxConcurrentCalls = 2
};
// create a processor that we can use to process the messages
await using ServiceBusProcessor processor = client.CreateProcessor(queueName, options);
// configure the message and error handler to use
processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;
async Task MessageHandler(ProcessMessageEventArgs args)
{
string body = args.Message.Body.ToString();
Console.WriteLine(body);
// we can evaluate application logic and use that to determine how to settle the message.
await args.CompleteMessageAsync(args.Message);
}
Task ErrorHandler(ProcessErrorEventArgs args)
{
// the error source tells me at what point in the processing an error occurred
Console.WriteLine(args.ErrorSource);
// the fully qualified namespace is available
Console.WriteLine(args.FullyQualifiedNamespace);
// as well as the entity path
Console.WriteLine(args.EntityPath);
Console.WriteLine(args.Exception.ToString());
return Task.CompletedTask;
}
// start processing
await processor.StartProcessingAsync();
推荐阅读
- macos - 有没有办法在 macOS 中读取 .env 文件?
- python - 将点列表转换为 SVG 三次分段贝塞尔曲线
- ios - iCloud 仪表板:无法将 CloudKit 架构部署到生产环境
- php - 分类过滤器的 show_option_all 数组设置
- python - 旋转单个 Kivy 元素
- r - 如何控制被 Word 重新缩放的 rmarkdown word 文档图像?
- ios - 更新我的 XCode 后,我收到一个 Strideable 错误
- windows - 无权下载到 PC 上的 Program Files 文件夹
- javascript - 无法设置未定义 JavaScript 的属性“0”
- xamarin - 如何以 sqliteconnection xamarin 形式保存图像