Exemplo n.º 1
0
        public static async Task Run([ServiceBusTrigger("createordertopic", "CreateOrder", AccessRights.Listen,
                                                        Connection = "ServiceBusConnectionString")] string topicMessage,

                                     TraceWriter log)
        {
            var topicClient = new Microsoft.Azure.ServiceBus.TopicClient(Environment.GetEnvironmentVariable("ServiceBusConnectionString"),
                                                                         Environment.GetEnvironmentVariable("CheckInventoryTopicName"));

            try
            {
                if (string.IsNullOrEmpty(topicMessage))
                {
                    throw new Exception("Service Bus Topic Message is null or empty");
                }

                var order = JsonConvert.DeserializeObject <Order>(topicMessage);

                if (order == null)
                {
                    throw new Exception("Order is missing");
                }

                if (order.Customer == null)
                {
                    throw new Exception("Order is missing Customer information");
                }

                if (order.Products == null)
                {
                    throw new Exception("Order is missing Product information");
                }

                if (order.Payment == null)
                {
                    throw new Exception("Order is missing Payment information");
                }

                order.Status      = "Created";
                order.CreatedDate = DateTime.UtcNow;
                order.UpdatedDate = DateTime.UtcNow;

                await InsertOrder(order);

                var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(order)));
                message.MessageId = Guid.NewGuid().ToString();

                await topicClient.SendAsync(message);

                log.Info($"CreateOrder function processed message successfully: {topicMessage}");
            }
            catch (Exception ex)
            {
                log.Error($"CreateOrder function Failed. Exception: {ex.Message}");
            }
            finally
            {
                await topicClient.CloseAsync();
            }
        }
Exemplo n.º 2
0
        public static async Task Run([ServiceBusTrigger("checkinventorytopic", "CheckOrderInventory", AccessRights.Listen,
                                                        Connection = "ServiceBusConnectionString")] string topicMessage, TraceWriter log)
        {
            var topicClient = new Microsoft.Azure.ServiceBus.TopicClient(Environment.GetEnvironmentVariable("ServiceBusConnectionString"),
                                                                         Environment.GetEnvironmentVariable("InventoryCheckedTopicName"));

            try
            {
                if (string.IsNullOrEmpty(topicMessage))
                {
                    log.Info($"ServiceBus topic message is null or empty.");
                    return;
                }

                var order = JsonConvert.DeserializeObject <Order>(topicMessage);

                if (order.Payment == null)
                {
                    throw new Exception("Order is missing Payment information.");
                }

                var isInventoryVerified = await IsInventoryVerified(order);

                Message message = new Message();

                if (isInventoryVerified)
                {
                    order.Status          = "Inventory Verified";
                    order.UpdatedDate     = DateTime.UtcNow;
                    message.MessageId     = Guid.NewGuid().ToString();
                    message.CorrelationId = "Verified";
                }
                else
                {
                    order.Status          = "Insufficient Inventory";
                    order.UpdatedDate     = DateTime.UtcNow;
                    message.MessageId     = Guid.NewGuid().ToString();
                    message.CorrelationId = "Insufficient";

                    log.Info($"Insufficient inventory to complete order.");
                }

                message.Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(order));

                await topicClient.SendAsync(message);

                log.Info($"CheckOrderInventory function processed message successfully: {topicMessage}");
            }
            catch (Exception ex)
            {
                log.Error($"CheckOrderInventory function Failed. Exception: {ex.Message}");
            }
            finally
            {
                await topicClient.CloseAsync();
            }
        }
Exemplo n.º 3
0
        public static async Task Run([ServiceBusTrigger("inventorycheckedtopic", "ProcessPayment",
                                                        AccessRights.Listen, Connection = "ServiceBusConnectionString")] string topicMessage, TraceWriter log)
        {
            var topicClient = new Microsoft.Azure.ServiceBus.TopicClient(Environment.GetEnvironmentVariable("ServiceBusConnectionString"),
                                                                         Environment.GetEnvironmentVariable("PaymentProcessedTopicName"));

            try
            {
                if (string.IsNullOrEmpty(topicMessage))
                {
                    throw new Exception("ServiceBus topic message is null or empty.");
                }

                var order = JsonConvert.DeserializeObject <Order>(topicMessage);

                var transaction = await InsertPaymentTransaction(order);

                var paymentSucceeded = await Task.FromResult <bool>(PaymentSucceeded());

                var message = new Message();

                if (paymentSucceeded)
                {
                    await UpdatePaymentTransaction(transaction, "Verified");

                    order.Status          = "Payment Verified";
                    order.UpdatedDate     = DateTime.UtcNow;
                    message.MessageId     = Guid.NewGuid().ToString();
                    message.CorrelationId = "Verified";
                }
                else
                {
                    await UpdatePaymentTransaction(transaction, "Failed");

                    order.Status          = "Payment Failed";
                    order.UpdatedDate     = DateTime.UtcNow;
                    message.MessageId     = Guid.NewGuid().ToString();
                    message.CorrelationId = "Failed";
                }

                message.Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(order));
                await topicClient.SendAsync(message);

                log.Info($"ProcessOrderPayment function processed message successfully: {topicMessage}");
            }
            catch (Exception ex)
            {
                log.Error($"ProcessOrderPayment Failed. Exception: {ex.Message}");
            }
            finally
            {
                await topicClient.CloseAsync();
            }
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public async Task SendAsync <TMessage>(TMessage message) where TMessage : IBusTopicMessage
        {
            if (string.IsNullOrEmpty(message.TopicName))
            {
                throw new ApplicationException("Topic name is mandatory");
            }

            var client = new Microsoft.Azure.ServiceBus.TopicClient(new ServiceBusConnectionStringBuilder(_connectionString)
            {
                EntityPath = message.TopicName
            });

            var serviceBusMessage = _messageFactory.CreateMessage(message);

            await client.SendAsync(serviceBusMessage);

            await client.CloseAsync();
        }