首页 > 解决方案 > 将表存储密钥作为消息放入 Azure 队列

问题描述

我能够将行键和分区键插入 Azure 表存储:

TableOperation insertOperation = TableOperation.Insert(customer1); 表。执行(插入操作);StoreKeyInQ(插入操作);

// Create a new customer entity.
        CustomerEntity customer1 = new CustomerEntity("URL", "Name"+Guid.NewGuid());
        customer1.path = fullpath;

“客户实体”如下:

public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string ID, string Name)
        {
            this.PartitionKey = ID;
            this.RowKey = Name;
        }

        public CustomerEntity() { }

        public string path { get; set; }


    }

我编写了代码来创建一个队列,如下所示:

 public void StoreKeyInQ(TableOperation insertOperation)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        //craete a queue client
        CloudQueueClient cloudQueueClient = storageAccount.CreateCloudQueueClient();
        //retrive a reference to a container
        CloudQueue cloudQueue = cloudQueueClient.GetQueueReference("myqueue");
        //create the queue if does not exists
        cloudQueue.CreateIfNotExists();

       // Create a message and add it to the queue.
       CloudQueueMessage message = new CloudQueueMessage(insertOperation); //Here is the error as "CloudQueueMessage" can't take a argument type "TableOperation"
       queue.AddMessage(message);
    }

如何解决此错误?如何将表存储的“rowkey”和“partition”键都传递到队列中?

  EDIT:

我已经添加了这两行进行序列化,可以吗?

 System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(customerEntity.GetType());
        CloudQueueMessage message = new CloudQueueMessage(x.ToString());

我的队列是这样显示的;但是,不确定这是否正确?谁能澄清一下-这是正确的吗?

在此处输入图像描述

标签: .netazureazure-storageazure-table-storageazure-storage-queues

解决方案


如何解决此错误?

如果我们想将消息插入到队列中。我们可以得到操作队列

来自使用 .NET 开始使用 Azure 队列存储的代码演示

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference("myqueue");

// Create the queue if it doesn't already exist.
queue.CreateIfNotExists();

// Create a message and add it to the queue.
CloudQueueMessage message = new CloudQueueMessage("RowKey, PartitionKey ");
queue.AddMessage(message);

如何将表存储的“rowkey”和“partition”键都传递到队列中?

您可以将模型序列化为字符串,然后将其保存为消息,或者您可以使用自定义格式来保存消息,例如消息格式:RowKey, PartitionKey

基本上想法是将 RowKey+PartitionKey 存储在队列中并创建一个工作角色以从队列中检索密钥 (RowKey+PartitionKey )。

如果您只想检索队列消息,我们可以使用许多便宜的方法,例如Webjob queue triggerazure function queue trigger

更新:

请尝试使用以下代码。

using Newtonsoft.Json;
CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject(customer1));
cloudQueue.AddMessage(message);

在此处输入图像描述

有关更多演示代码,请参阅SerializeObjectDeserialize an Object。


推荐阅读