gpt4 book ai didi

c# - 有时调用 CompleteAsync 方法时,Azure 设备客户端会抛出 DeviceMessageLockLostException

转载 作者:行者123 更新时间:2023-12-03 05:30:22 26 4
gpt4 key购买 nike

我正在尝试从 Azure 设备客户端接收消息,如下所示

public async Task<List<string>> RecieveMessage(string correlationId)
{
var response = new List<string>();
InitializeDeviceClient("AMQP");
var flag = true;
while (flag)
{
Microsoft.Azure.Devices.Client.Message receivedMessage = await deviceClient.ReceiveAsync(TimeSpan.FromSeconds(120));

if (receivedMessage == null)
{
await Task.Delay(100).ConfigureAwait(false);
continue;
}

Trace.WriteLine(receivedMessage.CorrelationId.ToString());
await this.deviceClient.CompleteAsync(receivedMessage);
if (receivedMessage.CorrelationId != correlationId)
{
continue;
}

var content = Encoding.UTF8.GetString(receivedMessage.GetBytes());
response.Add(content);
flag = false;
}
return response;
}

当我根据特定的“CorrelationId”过滤消息时,我会返回响应。

当我从 VSTS 运行时,在调用“CompleteAsync”的步骤中会间歇性地看到异常。

Microsoft.Azure.Devices.Client.Exceptions.DeviceMessageLockLostException: Exception of type 'Microsoft.Azure.Devices.Client.Exceptions.DeviceMessageLockLostException' was thrown.

堆栈跟踪:

Microsoft.Azure.Devices.Client.Transport.AmqpIoT.AmqpIoTOutcome.ThrowIfError()
at Microsoft.Azure.Devices.Client.Transport.Amqp.AmqpTransportHandler.DisposeMessageAsync(String lockToken, AmqpIoTDisposeActions outcome)
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<>c__DisplayClass23_0.<<ExecuteWithErrorHandlingAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.ExecuteWithErrorHandlingAsync[T](Func`1 asyncOperation)
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.<>c__DisplayClass26_0.<<CompleteAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.CompleteAsync(String lockToken, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.InternalClient.CompleteAsync(String lockToken)

最佳答案

根据GH issue#117#111 ,当 C2D 消息的 TTL 在设备确认之前过期时,DeviceMessageLockLostException 是正常操作的一部分。

我将在这里添加推荐:

When you get a DeviceMessageLockLostException, the calling applicationshould just trace it, ignore it and proceed. One of two things willhappen:

DeviceClient receives the same message again on ReceiveAsync(). Atthis point, the DeviceClient can try to complete the message again.

DeviceClient never receives the same message again, in which case noaction required anyways.

关于c# - 有时调用 CompleteAsync 方法时,Azure 设备客户端会抛出 DeviceMessageLockLostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65951483/

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