我正在尝试使用以下代码更新Azure队列的内容:
queue.UpdateMessage(new CloudQueueMessage(timestamp + "|" + encodedContent),TimeSpan.FromMinutes(0),MessageUpdateFields.Content, null, null);
但是,我不断收到以下错误“值不能为空。参数名称:messageId”。 尝试通过互联网进行搜索,但找不到任何遇到此问题的人。
注意:我必须使用Azure队列。
答案 0 :(得分:0)
要更新消息,您需要先获取特定消息,而不是创建新消息。
答案 1 :(得分:0)
更新:
您可以将消息ID和PopReceipt传递给您的方法:
static void Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a container.
CloudQueue queue = queueClient.GetQueueReference("myqueue0905");
//Retrieve the cached approximate message count.
queue.FetchAttributes();
int? cachedMessageCount = queue.ApproximateMessageCount;
//get the message id and PopReceipt, or you can pass them from your api call.
string _id = "";
string _popreceipt = "";
if (cachedMessageCount != null)
{
foreach (CloudQueueMessage message in queue.GetMessages(cachedMessageCount.Value, TimeSpan.FromMinutes(3)))
{
_id = message.Id;
_popreceipt = message.PopReceipt;
Console.WriteLine("the id is:" + _id);
Console.WriteLine("the pop receipt is:" + _popreceipt);
}
}
//update queue message by using message id and PopReceipt
var msg = new CloudQueueMessage(_id, _popreceipt);
msg.SetMessageContent("a test messge 123456");
queue.UpdateMessage(msg, TimeSpan.FromSeconds(120.0),
MessageUpdateFields.Content | MessageUpdateFields.Visibility);
Console.WriteLine("complete update");
Console.ReadLine();
}
以您为例,假设您已经知道消息ID,则可以通过该ID更新消息。
我们需要使用消息ID遍历所有队列消息,以获取要更新的正确消息。
代码如下:
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System;
namespace ConsoleApp11
{
class Program
{
static void Main(string[] args)
{
//assume you know the message id
string message_id = "3798f6b6-1541-4421-b8de-79a8294edf49";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a container.
CloudQueue queue = queueClient.GetQueueReference("myqueue0905");
//Retrieve the cached approximate message count.
queue.FetchAttributes();
int? cachedMessageCount = queue.ApproximateMessageCount;
//if the queue is not null
if (cachedMessageCount != null)
{
foreach (CloudQueueMessage message in queue.GetMessages(cachedMessageCount.Value))
{
if (message.Id == message_id)
{
message.SetMessageContent("test updated content again");
queue.UpdateMessage(message, TimeSpan.FromSeconds(5.0),
MessageUpdateFields.Content | MessageUpdateFields.Visibility);
}
}
}
Console.ReadLine();
}
}
}