Rade Milovic
Rade Milovic

Reputation: 1015

How to send queue message from Windows Phone?

I'm using the Windows Azure toolkit for Windows Phone, but I have difficulities to send a queue message to the cloud service. Here is my code:

public void SendQueueMessage(string queueReference, params string[] message)
    {
        CloudQueue cloudQueue = new CloudQueue();
        CloudQueueClient queueClient = new CloudQueueClient(queueUri, credentials);
        queueClient.GetQueueReference(queueReference);

        StringBuilder sb = new StringBuilder();
        foreach (var messagePart in message)
        {
            sb.Append(messagePart);
            sb.Append(":");
        }
        sb.Remove(sb.Length - 2, 1);
        CloudQueueMessage queueMessage = new CloudQueueMessage { AsBytes = Encoding.UTF8.GetBytes(sb.ToString()) };
        cloudQueue.AddMessage(queueMessage, r => this.dispatcher.BeginInvoke(() => 
                                                                             {
                                                                                 if(r.Exception != null)
                                                                                 {
                                                                                     //handle exception
                                                                                 }
                                                                             }));
    }

I'm always getting null exception at AddMessage method.Any ideas?

Upvotes: 1

Views: 342

Answers (1)

James Mundy
James Mundy

Reputation: 4329

I suspect you may have found out how to do this by now but after much looking I found how to add a queue to storage from Windows Phone via this episode of cloudcove r. You should be looking at around the 13:44 minute mark.

http://www.joshholmes.com/blog/2012/01/18/using-windows-azure-storage-from-the-windows-phone/

Hopefully, this will help you out

Edit: Having copied the code from the video it should look something like this:

  var queueClient = CloudStorageContext.Current.Resolver.CreateCloudQueueClient();
        var queue = queueClient.GetQueueReference("imagestodo");
        queue.Create(r => queue.AddMessage(
               new CloudQueueMessage
               {
                   AsBytes = Encoding.UTF8.GetBytes(imageID),
                   Id = imageID
               },
               c =>
                   {
                       //logic here
                   }));

Upvotes: 1

Related Questions