问题描述
当前,我正在将 Microsoft.Azure.ServiceBus.IQueueClient
用于 RegisterMessageHandler
,然后收到的消息的类型为 Microsoft.Azure.ServiceBus.消息
.
Current I'm using Microsoft.Azure.ServiceBus.IQueueClient
to RegisterMessageHandler
, and then the message I receive is of type Microsoft.Azure.ServiceBus.Message
.
根据文档:
...但是这些库中没有一个与我实际使用的类相关.我该如何延期?为了能够延迟消息,我必须使用什么类和东西?上面的所有示例都没有给出足够的代码片段来解释它.
...but none of those libraries seam to relate to the classes I'm actually using. How do I defer? What classes and stuff do I have to use in order to be able to defer messages? All the samples above dont give enough code snippets to explain it.
根据@Gaurav的要求
Update as requested by @Gaurav
从您的答案中,我可以看到我的消息具有该属性:
from your answer, I can see my message has that property:
message.ScheduledEnqueueTimeUtc = DateTime.UtcNow.AddHours(1);
,但是 queueClient
也具有此方法:
queueClient.ScheduleMessageAsync(message, DateTime.UtcNow.AddHours(1));
我要尝试使用 scheduledMessageAsync
,因为我看不到如何传达已设置 ScheduledEnqueueTimeUtc
的信息,而无需调用 queueClient
I'm going to try 'scheduledMessageAsync
' as I cant see how to communicate that I've set ScheduledEnqueueTimeUtc
without calling the queueClient
推荐答案
我已经编码了所需的解决方案,这是基本概述:
I've coded the solution I was looking for, here is the basic outline:
在异步方法内部(运行自己的线程)
inside an asynchronous method (runs its own thread)
public async Task InitialiseAndRunMessageReceiver()
启动读取消息的无限循环
start an infinite loop that reads the message
receiver = new MessageReceiver(serviceBusConnectionString, serviceBusQueueName, ReceiveMode.PeekLock);
while (true) { var message = await receiver.ReceiveAsync(); ... more code... }
一旦您知道您将要开始长任务,请推迟消息,但要存储 message.SystemProperties.SequenceNumber
.这样可以将其保留在队列中,但会阻止重新发送它.
once you know you are about to start your long task, defer the message, but store the message.SystemProperties.SequenceNumber
. this keeps it in the queue but prevents it from being re-delivered.
await receiver.DeferAsync(message.SystemProperties.LockToken);
,当您最终完成操作时,请使用 message.SystemProperties.SequenceNumber
再次询问消息,并像未延迟一样完成消息
and when you finally done ask for the message again using the message.SystemProperties.SequenceNumber
, and complete the message as if it weren't deferred
var message = receiver.ReceiveDeferredMessageAsync(message.SystemProperties.SequenceNumber);
receiver.CompleteAsync(message.Result.SystemProperties.LockToken);
,您的消息将从队列中删除.
and your message will be removed from the queue.
我的大部分困惑是由于库的命名相似,使用寿命重叠而引起的.
much of my confusion was caused by the libraries being named similarly with overlapping lifespans.
Microsoft.Azure.ServiceBus.Core.MessageReceiver
是上方的消息接收器
这篇关于如何推迟Azure服务总线消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!