private async Task RunAsync() { BrokeredMessage receivedMessage = null; while (!IsStopped) { try { // Receive the message receivedMessage = SubClient.Receive(); if (receivedMessage != null) { Order orderDetails = receivedMessage.GetBody<Order>(); Order order = new Order(); order.CustomerName = orderDetails.CustomerName; order.EmailId = orderDetails.EmailId; order.ProductOrderDetailsList = orderDetails.ProductOrderDetailsList; order.OrderDate = orderDetails.OrderDate; order.TotalDue = orderDetails.TotalDue; order.orderStatus = "Processed"; // Remove message from subscription receivedMessage.Complete(); order.OrderId = Guid.NewGuid(); await azureDocDBHelper.AddDocument(order, "OrderDetails"); receivedMessage = null; } } catch (MessagingException e) { if (null != receivedMessage) { //unlock message in subscription receivedMessage.Abandon(); } if (!e.IsTransient) { Trace.WriteLine(e.Message); throw; } Thread.Sleep(10000); } catch (Exception ex) { // unlock message in subscription receivedMessage.Abandon(); Trace.WriteLine(ex.Message); } } }
public ActionResult SubmitOrder(ProductOrder productOrder) { //if (ModelState.IsValid) { try { ViewBag.SubmitResponse = string.Empty; AzureGuidance.Domain.Order order = new AzureGuidance.Domain.Order(); order.CustomerName = productOrder.order.CustomerName; order.OrderDate = DateTime.Now; order.EmailId = productOrder.order.EmailId; order.ProductOrderDetailsList = new List<ProductDetails>(); foreach (AddProduct p in productOrder.lstProducts) { if (true == p.SelectProduct) { ProductDetails productDetails = new ProductDetails(); productDetails.ProductName = p.ProductName; productDetails.ProductQuantity = p.ProductQuantity; order.TotalDue += p.UnitPrice * p.ProductQuantity; order.orderStatus = "Processed"; order.OrderId = Guid.NewGuid(); order.ProductOrderDetailsList.Add(productDetails); p.ProductQuantity = 0; p.SelectProduct = false; } } var message = new Microsoft.ServiceBus.Messaging.BrokeredMessage(order); if (null == ServiceBusTopicHelper.OrdersTopicClient) { ServiceBusTopicHelper.Initialize(); } ServiceBusTopicHelper.OrdersTopicClient.Send(message); } catch (Exception ex) { Console.WriteLine(ex.Message); } } ProductOrder pOrder = new ProductOrder(); List<AddProduct> lstProducts = new List<AddProduct>(); pOrder.lstProducts = productOrder.lstProducts; ModelState.Clear(); ViewBag.SubmitResponse = "Order proccessed successfully."; return View("DisplayProducts", pOrder); }
public static async Task ProcessTopicMessage([ServiceBusTrigger("OrderTopic", "OrderMessagesForNotification")] BrokeredMessage message, TextWriter logger) { logger.WriteLine("Topic message: " + message); if (message != null) { Order orderDetails = message.GetBody<Order>(); Order order = new Order(); order.CustomerName = orderDetails.CustomerName; order.OrderId = orderDetails.OrderId; await SendNotificationAsync(order, logger); // Remove message from subscription message.Complete(); message = null; } }
public override void Run() { BrokeredMessage receivedMessage = null; while (!IsStopped) { try { // Receive the message if (null != NotificationSubClient) { receivedMessage = NotificationSubClient.Receive(); if (receivedMessage != null) { Order orderDetails = receivedMessage.GetBody<Order>(); Order order = new Order(); order.CustomerName = orderDetails.CustomerName; order.OrderId = orderDetails.OrderId; SendNotificationAsync(order); // Remove message from subscription receivedMessage.Complete(); receivedMessage = null; } } } catch (MessagingException e) { //unlock message in subscription receivedMessage.Abandon(); if (!e.IsTransient) { Trace.WriteLine(e.Message); throw; } Thread.Sleep(10000); } catch (Exception ex) { // unlock message in subscription receivedMessage.Abandon(); Trace.WriteLine(ex.Message); } } }
public ActionResult SubmitOrder(ProductOrder productOrder) { //if (ModelState.IsValid) { try { ViewBag.SubmitResponse = string.Empty; AzureGuidance.Domain.Order order = new AzureGuidance.Domain.Order(); order.CustomerName = productOrder.order.CustomerName; order.OrderDate = DateTime.Now; order.EmailId = productOrder.order.EmailId; order.ProductOrderDetailsList = new List <ProductDetails>(); foreach (AddProduct p in productOrder.lstProducts) { if (true == p.SelectProduct) { ProductDetails productDetails = new ProductDetails(); productDetails.ProductName = p.ProductName; productDetails.ProductQuantity = p.ProductQuantity; order.TotalDue += p.UnitPrice * p.ProductQuantity; order.orderStatus = "Processed"; order.OrderId = Guid.NewGuid(); order.ProductOrderDetailsList.Add(productDetails); p.ProductQuantity = 0; p.SelectProduct = false; } } var message = new Microsoft.ServiceBus.Messaging.BrokeredMessage(order); if (null == ServiceBusTopicHelper.OrdersTopicClient) { ServiceBusTopicHelper.Initialize(); } ServiceBusTopicHelper.OrdersTopicClient.Send(message); } catch (Exception ex) { Console.WriteLine(ex.Message); } } ProductOrder pOrder = new ProductOrder(); List <AddProduct> lstProducts = new List <AddProduct>(); pOrder.lstProducts = productOrder.lstProducts; ModelState.Clear(); ViewBag.SubmitResponse = "Order proccessed successfully."; return(View("DisplayProducts", pOrder)); }
private static async Task SendNotificationAsync(Order varOrder, TextWriter logger) { try { DateTime timeNow = DateTime.Now; string template = string.Format("{0} {1} {2} {3} {4}", "{ \"data\" : {\"message\":\"Order: ", varOrder.OrderId, "by", varOrder.CustomerName, "\"}}"); string endpointUrl = "Endpoint=sb://azureguidancenotificationns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=i9u54hy58z80jyUO5oNAnfQ7rJhuwhilij7je5pHCuw="; NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(endpointUrl, "AzureGuidanceNotification"); var outcome = await hub.SendGcmNativeNotificationAsync(template); } catch (Exception ex) { // } }
private static async Task SendNotificationAsync(Order varOrder, TextWriter logger) { try { DateTime timeNow = DateTime.Now; string template = string.Format("{0} {1} {2} {3} {4}", "{ \"data\" : {\"message\":\"Order: ", varOrder.OrderId, "by", varOrder.CustomerName, "\"}}"); string endpointUrl = ""; NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(endpointUrl, "AzureGuidanceNotification"); //NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("Endpoint=sb://dericknelson.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=ON34fGIXG7LTA+WlZNXvxDMBZKIH2H6UUI9v2ASeT24=", "phoenixnotifhub"); var outcome = await hub.SendGcmNativeNotificationAsync(template); } catch (Exception ex) { // } }
//AzureDocumentDBHelper azureDocDBHelper; // This function will get triggered/executed when a new message is send to the topic public static async Task ProcessTopicMessage([ServiceBusTrigger("OrderTopic", "OrderMessagesForDocumentDB")] BrokeredMessage message, TextWriter logger) { logger.WriteLine("Topic message: " + message); if (message != null) { Order orderDetails = message.GetBody<Order>(); Order order = new Order(); order.CustomerName = orderDetails.CustomerName; order.EmailId = orderDetails.EmailId; order.ProductOrderDetailsList = orderDetails.ProductOrderDetailsList; order.OrderDate = orderDetails.OrderDate; order.TotalDue = orderDetails.TotalDue; order.orderStatus = "Processed"; //Remove message from subscription message.Complete(); await AzureDocumentDBHelper.AddDocument(order, "OrderDetails"); message = null; } }
public static bool AddOrder(Order order) { bool orderAdded = false; var ordr = new Order(); ordr.OrderId = order.OrderId; ordr.ProductOrderDetailsList = order.ProductOrderDetailsList; ordr.TotalDue = order.TotalDue; ordr.CustomerName = order.CustomerName; ordr.EmailId = order.EmailId; ordr.orderStatus = order.orderStatus; ordr.OrderDate = order.OrderDate; using (HttpClient httpClient = new HttpClient()) { httpClient.BaseAddress = new Uri("https://microsoft-apiappc2c97ea690dc4a59ab2bc747063c4387.azurewebsites.net/"); var url = "api/Orders"; var response = httpClient.PostAsJsonAsync(url, ordr).Result; if (response.IsSuccessStatusCode) { orderAdded = true; } } return orderAdded; }
private static async void SendNotificationAsync(Order varOrder) { try { DateTime timeNow = DateTime.Now; //string template = "{\"data\":{\"message\":\"Notification Hub test notification\"}}"; string template = string.Format("{0} {1} {2} {3} {4}", "{ \"data\" : {\"message\":\"Order: ", varOrder.OrderId, "by", varOrder.CustomerName, "\"}}"); NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("", "azureguidancehub"); var outcome = await hub.SendGcmNativeNotificationAsync(template/*, "mySampleTag"*/); } catch(Exception ex) { Trace.WriteLine(ex.Message); } }