コード例 #1
0
        public override void Run()
        {
            //Read messages
            while (true)
            {
                BrokeredMessage inputMessage  = null;
                Stream          messageStream = null;
                string          messageBody;
                try
                {
                    inputMessage = _inputQueueClient.Receive();

                    Trace.WriteLine(string.Format("Message received: {0}, {1}", inputMessage.SequenceNumber, inputMessage.MessageId));

                    inputMessage.Complete();

                    //Retrieve meessage body
                    messageStream = inputMessage.GetBody <Stream>();
                    TextReader reader = new StreamReader(messageStream, true);
                    //Problem with schema
                    messageBody = reader.ReadToEnd();
                    var inputMessageObject = JsonConvert.DeserializeObject <Message>(messageBody);

                    //Perform request to third-party notification service. Skeleton implementation
                    IPublishNotificationMessageManager messageManager = new PubNubNotificationMessageManager();
                    messageManager.Publish(inputMessageObject);
                }
                catch (Exception ex)
                {
                    //Handle failure and send message. Post a copy of the input message to error queue.
                    if (inputMessage != null)
                    {
                        var errorMessage = new BrokeredMessage(messageStream, true);
                        //We are adding and information about the occured exeption.
                        errorMessage.Properties.Add("Exception", ex.Message);
                        errorMessage.Properties.Add("ExceptionStackTrace", ex.StackTrace);

                        _errorQueueClient.Send(errorMessage);
                    }
                }
                finally
                {
                    if (messageStream != null)
                    {
                        messageStream.Close();
                    }
                }

                Thread.Sleep(3000);
            }
// ReSharper disable FunctionNeverReturns
        }
コード例 #2
0
ファイル: WorkerRole.cs プロジェクト: ppenchev/Test-Project
        public override void Run()
        {
            //Read messages
            while (true)
            {
                BrokeredMessage inputMessage = null;
                Stream messageStream = null;
                string messageBody;
                try
                {

                    inputMessage = _inputQueueClient.Receive();

                    Trace.WriteLine(string.Format("Message received: {0}, {1}", inputMessage.SequenceNumber, inputMessage.MessageId));

                    inputMessage.Complete();

                    //Retrieve meessage body
                    messageStream = inputMessage.GetBody<Stream>();
                    TextReader reader = new StreamReader(messageStream, true);
                    //Problem with schema
                    messageBody = reader.ReadToEnd();
                    var inputMessageObject = JsonConvert.DeserializeObject<Message>(messageBody);

                    //Perform request to third-party notification service. Skeleton implementation
                    IPublishNotificationMessageManager messageManager = new PubNubNotificationMessageManager();
                    messageManager.Publish(inputMessageObject);
                }
                catch (Exception ex)
                {
                    //Handle failure and send message. Post a copy of the input message to error queue.
                    if (inputMessage != null)
                    {
                        var errorMessage = new BrokeredMessage(messageStream, true);
                        //We are adding and information about the occured exeption.
                        errorMessage.Properties.Add("Exception", ex.Message);
                        errorMessage.Properties.Add("ExceptionStackTrace", ex.StackTrace);

                        _errorQueueClient.Send(errorMessage);
                    }
                }
                finally
                {
                    if (messageStream != null) messageStream.Close();
                }

                Thread.Sleep(3000);
            }
            // ReSharper disable FunctionNeverReturns
        }