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();
            }
        }
示例#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();
            }
        }
示例#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();
            }
        }
        private void ATestTopicAndATestSubscription()
        {
            testTopicClient        = new TopicClient(ServiceBusConnectionString, TopicName);
            testSubscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName);

            // create test subscription
            namespaceManager = NamespaceManager.CreateFromConnectionString(ServiceBusConnectionString);
            namespaceManager.CreateSubscription(TopicName, TEST_MESSAGE_SUB);

            options = new OnMessageOptions()
            {
                AutoComplete = false, AutoRenewTimeout = TimeSpan.FromMinutes(1),
            };
        }
示例#5
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();
        }
示例#6
0
 public static TopicClient Create(ServiceBusEntityConnection entityConnection)
 {
     return(TopicClient.Create(entityConnection, ReceiveMode.PeekLock));
 }