How to send queue message from Windows Phone?

99封情书 提交于 2019-12-11 09:03:35

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/9971393/how-to-send-queue-message-from-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!