.net - 将表存储密钥作为消息放入 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());
我的队列是这样显示的;但是,不确定这是否正确?谁能澄清一下-这是正确的吗?
解决方案
如何解决此错误?
如果我们想将消息插入到队列中。我们可以得到操作队列
来自使用 .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 trigger或azure function queue trigger。
更新:
请尝试使用以下代码。
using Newtonsoft.Json;
CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject(customer1));
cloudQueue.AddMessage(message);
有关更多演示代码,请参阅SerializeObject或Deserialize an Object。
推荐阅读
- javascript - Aurelia/Karma/Istanbul 无法读取未定义的属性“skip”和“decl”,但会产生融洽的关系
- apache-nifi - Apache NiFi:使用映射值将列添加到 csv
- r - 如何rbind(连接)存储为列表元素的数据帧
- arrays - 如何不在列表飞镖中重复相同的项目?
- javascript - 更新从 js 文件加载的对象中的属性 - javascript
- linux - Docker 加载代理环境变量失败
- angular - Angular Material Spinner 未显示,但存在于 DOM 中
- angular - 一些node_modules在本地有角度后没有推入git
- spring - 如何使用spring Data JPA将实体映射为java.util.Map?
- r - 清理电子邮件正文/文本以在 R 中进行情绪分析