Exemplo n.º 1
0
        // Send messages.
        //SendMessagesAsync(numberOfMessages);
        //topicClient.CloseAsync();

        public async Task <string> SendMessagesAsync(PromotionMessage promotionMessage)
        {
            topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
            try
            {
                // Create a new message to send to the topic
                var message = new Message(Encoding.UTF8.GetBytes($"{promotionMessage.UserName}," +
                                                                 $"{promotionMessage.EmailAddress},{promotionMessage.PhoneNumber}"));
                List <Message> messages = new List <Message>()
                {
                    new Message(Encoding.UTF8.GetBytes(promotionMessage.UserName)),
                    new Message(Encoding.UTF8.GetBytes(promotionMessage.EmailAddress)),
                    new Message(Encoding.UTF8.GetBytes(promotionMessage.PhoneNumber))
                };

                //public override string ToString() => $"{}";

                // Send the message to the topic
                await topicClient.SendAsync(message);

                return("Message has been published");
            }
            catch (Exception exception)
            {
                return($"{DateTime.Now} :: Exception: {exception.Message}");
            }
            finally
            {
                await topicClient.CloseAsync();
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Message([Bind("UserName,EmailAddress,PhoneNumber")] PromotionMessage message)
        {
            if (ModelState.IsValid)
            {
                string result = await _publish.SendMessagesAsync(message);

                //return RedirectToAction(nameof(PromotionMessage));
                ViewData["response"] = result;
                return(View());
            }
            ;
            return(View(message));
        }
Exemplo n.º 3
0
        public async Task <string> SendMessagesAsync(PromotionMessage promotionMessage)
        {
            try
            {
                HttpResponseMessage response = await client.PostAsJsonAsync("http://localhost:5000/api/message", promotionMessage);

                response.EnsureSuccessStatusCode();
                return("Message has been published");
            }
            catch (Exception exception)
            {
                return($"{DateTime.Now} :: Exception: {exception.Message}");
            }
        }
        public async Task <string> Post([FromBody] PromotionMessage promotionMessage)
        {
            string result = await _publish.SendMessagesAsync(promotionMessage);

            return(result);
        }