gpt4 book ai didi

c# - 当我处于 ReceiveAndDelete 模式时,是否需要调用 CompleteMessageAsync?

转载 作者:行者123 更新时间:2023-12-02 23:24:20 30 4
gpt4 key购买 nike

我有以下 Azure 服务总线代码:

private async Task startInternalAsync(CancellationToken cancellationToken) {
await using var client = new ServiceBusClient(_connectionString);
try {
var processor = client.CreateProcessor(_queueName, new ServiceBusProcessorOptions {
ReceiveMode = ServiceBusReceiveMode.ReceiveAndDelete,
PrefetchCount = 100
});

processor.ProcessMessageAsync += messageHandler;
processor.ProcessErrorAsync += errorHandler;

await processor.StartProcessingAsync(cancellationToken);
await cancellationToken.WhenCanceled();

_logger.LogDebug("Cancellation requested. Stopping the receiver...");
await processor.StopProcessingAsync();
_logger.LogDebug("Stopped receiving messages.");
}
catch (TaskCanceledException) {
_logger.LogDebug("Cancellation requested. Exiting.");
}
catch (Exception ex) {
_logger.LogError("Message processing exception. Exiting.", ex.Message);
}
}

private async Task messageHandler(ProcessMessageEventArgs args) {
string body = args.Message.Body.ToString();
_logger.LogDebug($"Received msg: {body}");

// complete the message. message is deleted from the queue.
await args.CompleteMessageAsync(args.Message);
}

private async Task errorHandler(ProcessErrorEventArgs args) {
await Task.Yield();
_logger.LogError(args.Exception.ToString());
}

我真的需要在 messageHandler 中调用 await args.CompleteMessageAsync(args.Message) 吗?我处于 ReceiveAndDelete 模式,因此在检索时应立即从 ASB 队列中删除消息,但仍需要 CompleteMessageAsync() 才能将其从本地队列中删除?或者我可以删除该调用吗?

最佳答案

您无需调用CompleteMessageAsync,因为消息一旦发送就会自动删除。

事实上,我记得在 ReceiveAndDelete 模式下获取消息时,调用此方法实际上会引发错误。

关于c# - 当我处于 ReceiveAndDelete 模式时,是否需要调用 CompleteMessageAsync?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67333684/

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