Ключ хранения таблицы в виде сообщения в очередь Azure - PullRequest
0 голосов
/ 09 мая 2018

Я могу вставить ключ строки и ключ раздела в хранилище таблиц Azure:

TableOperation insertOperation = TableOperation.Insert (customer1); table.Execute (insertOperation); StoreKeyInQ (insertOperation);

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

CustomerEntity выглядит следующим образом:

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());

Моя очередь показывает вот так; однако, не уверен, правильно ли это или нет? Может кто-нибудь уточнить, верно ли это?

enter image description here

1 Ответ

0 голосов
/ 09 мая 2018

Как устранить эту ошибку?

Если мы хотим вставить сообщение в очередь. Мы могли бы получить очередь операций

демонстрация кода из Начало работы с хранилищем очереди Azure с использованием .NET

// 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 или Триггер очереди функции Azure .

Обновление:

Пожалуйста, попробуйте использовать следующий код.

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

enter image description here

Более подробный демонстрационный код приведен в SerializeObject или Десериализация объекта.

...