示例#1
0
        private static void ReceiveMessages()
        {
            Console.WriteLine("\nReceiving message from Queue...");
            Console.WriteLine($"Connectionstring = {CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString")}");
            var helper      = new BusHelper();
            var queueClient = helper.CreateQueueClient();

            queueClient.OnMessage(message =>
            {
                try
                {
                    var body = message.GetBody <Applicant>();

                    Console.WriteLine(
                        $"Message received: Id = {message.MessageId}; Custom Property={message.Properties[Constants.CustomProperties.Custom1.ToString()]}; Body = {body}");
                    message.Complete();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    message.Abandon();
                }
            }, new OnMessageOptions
            {
                AutoComplete = false
            });
        }
        private static void SendMessages()
        {
            var helper      = new BusHelper();
            var queueClient = helper.CreateQueueClient();
            var topicClient = helper.CreateTopicClient();
            var messages    = CreateQueueMessages(50, CreateMessagePayload());

            Console.WriteLine("\nSending messages to Queue...");
            var rand = new Random(1000);

            foreach (var message in messages)
            {
                try
                {
                    var topicMessage = message.Clone();
                    //for simplicity clone the message and send the event
                    topicClient.Send(topicMessage);
                    queueClient.Send(message);
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                    HandleTransientErrors(e);
                }
                Console.WriteLine($"Message received: Id = {message.MessageId} ApplicationType = {message.Properties[Constants.CustomProperties.ApplicationType.ToString()]}");
                // sleep for a short period of time
                Thread.Sleep(rand.Next(500, 2000));
            }
        }