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

Do I need to call CompleteMessageAsync when I'm in ReceiveAndDelete mode?

我有以下 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模式下取消息的时候调用这个方法其实是报错的