public void SendMessage(ContosoMessage message)
        {
            BrokeredMessage bm = new BrokeredMessage();

            bm.Properties["timestamp"] = message.MessageTimestamp;
            bm.Properties["message"]   = message.MessageString;
            bm.MessageId = message.MessageID;
            queueClient.Send(bm);
        }
        // GET: ExpensiveTask
        public ActionResult PerformExpensiveTask()
        {
            var end = DateTime.Now + TimeSpan.FromSeconds(3);

            while (DateTime.Now < end)
            {
                //do nothing
            }
            List <ContosoMessage> messages = new List <ContosoMessage>();

            for (int i = 0; i < 10; i++)
            {
                ContosoMessage message = new ContosoMessage();
                message.MessageID        = (new Random()).Next().ToString();
                message.MessageString    = (new Random()).Next().ToString() + (new Random()).Next().ToString();
                message.MessageTimestamp = "" + (DateTime.Now.Ticks / (decimal)TimeSpan.TicksPerMillisecond);
                messages.Add(message);
            }
            return(View(messages));
        }
        public List <ContosoMessage> ReceiveMessages()
        {
            List <ContosoMessage> messageList = new List <ContosoMessage>();

            int             i = 0;
            BrokeredMessage m = null;

            do
            {
                i++;
                try
                {
                    m = queueClient.Receive(TimeSpan.FromSeconds(2));
                    ContosoMessage cm = new ContosoMessage();
                    cm.MessageID = m.MessageId;
                    Stream       stream   = m.GetBody <Stream>();
                    StreamReader reader   = new StreamReader(stream);
                    string       body     = reader.ReadToEnd();
                    var          bodyDict = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <String, String> >(body);
                    cm.MessageID        = bodyDict["messageId"];
                    cm.MessageTimestamp = bodyDict["timestamp"];
                    cm.MessageString    = bodyDict["message"];
                    messageList.Add(cm);
                    m.Complete();
                }
                catch (Exception e)
                {
                    m = null;
                }
            } while (i < 7 && m != null);

            /*
             * IEnumerable<BrokeredMessage> messages = queueClient.ReceiveBatch(10);
             * foreach(BrokeredMessage m in messages)
             * {
             * }
             * /* */
            return(messageList);
        }