值不能为空。参数名称:messageId

Value cannot be null. Parameter name: messageId

我正在尝试使用以下代码更新 Azure 队列的内容:

queue.UpdateMessage(new CloudQueueMessage(timestamp + "|" + encodedContent),TimeSpan.FromMinutes(0),MessageUpdateFields.Content, null, null);

但是,我不断收到以下错误 "Value cannot be null. Parameter name: messageId"。 尝试通过互联网搜索但找不到任何人遇到此问题。

注意:我必须使用 Azure Queue。

要更新消息,您需要先获取特定消息,而不是创建新消息。

更多详情Change the contents of a queued message

更新:

您可以将消息 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();
        }

    }
}