简体   繁体   中英

Azure Service Bus Read Message Queue

I am fairly new to Azure but struggling to devise or find a way of reading messages in my queue. I have the following:

  • Cloud Service
  • Storage Account
  • Queues

What I am struggling with now is, I can see via the pretty graph Portal gives me messages are being received but I would like to see the contents of them and this seems impossible to do via the Portal at least.

So I started hand cracking some code to get these messages but this doesn't work either.

    var credentials = new StorageCredentials("account", "key");
    var storageAccount = new CloudStorageAccount(credentials, true);
    var queue = storageAccount.CreateCloudQueueClient();
    var messages = queue.GetQueueReference("orders").GetMessages(100, TimeSpan.FromHours(10), null, null);

What I don't get is do I need to associate my queue with the storage?

Cheers, DS.

Here is a simple example to retrieve messages from a queue. First you need to create a CloudStorageAccount, to reference a specific storage. Second you create a new CloudQueueClient, in order connect to your CloudStorageAccount. Once you have the CloudQueueClient, you could reference the queue and create it.

        // Your Storage credentials
        var credentials = new StorageCredentials("account", "key");


        var storageAccount = new CloudStorageAccount(credentials, true);

        // Create a new 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();

        // Send 10 messages to the queue
        for (int i = 0; i < 10; i++)
        {
            // Create a message and add it to the queue.
            CloudQueueMessage message = new CloudQueueMessage(string.Format("Hello, World {0}", i));
            queue.AddMessage(message);
        }

        // Read next 20 messages
        foreach (CloudQueueMessage message in queue.GetMessages(20, TimeSpan.FromMinutes(5)))
        {
            // Reading content from message
            Console.WriteLine(message.AsString);

            // Process all messages in less than 5 minutes, deleting each message after processing.
            queue.DeleteMessage(message);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM