public static SendGridMessage ChargeTheCustomer(
            [ActivityTrigger] WhatIfDemoDbDataContext.Policy policy, ILogger log)
        {
            string emailAddress = Environment.GetEnvironmentVariable(EmailAddressVariableName);

            if (string.IsNullOrEmpty(emailAddress))
            {
                log.LogWarning($"TestEmailAddress is not specified, so not sending email to (userId {policy.userId}) about policy {policy.id}");
                return(null);
            }

            log.LogWarning($"Sending email to {emailAddress} (userId {policy.userId}) about policy {policy.id}");

            var message = new SendGridMessage();

            message.SetFrom(emailAddress);
            message.AddTo(emailAddress);

            message.SetSubject($"Your regular payment for policy {policy.id} is due");
            message.AddContent("text/html", $"Please, pay ${policy.paymentAmount}");

            // custom monitoring with app insights
            telemetryClient.GetMetric("AverageRegularPayment").TrackValue((double)policy.paymentAmount);

            return(message);
        }
예제 #2
0
        public static SendGridMessage ChargeTheCustomer(
            [ActivityTrigger] WhatIfDemoDbDataContext.Policy policy, ILogger log)
        {
            string emailAddress = Environment.GetEnvironmentVariable(EmailAddressVariableName);

            log.LogWarning($"Sending email to {emailAddress} (userId {policy.userId}) about policy {policy.id}");

            var message = new SendGridMessage();

            message.SetFrom(emailAddress);
            message.AddTo(emailAddress);

            message.SetSubject($"Your regular payment for policy {policy.id} is due");
            message.AddContent("text/html", $"Please, pay ${policy.paymentAmount}");

            return(message);
        }
        public static async Task <WhatIfDemoDbDataContext.Policy> SavePolicyToDb(
            [ActivityTrigger] WhatIfDemoDbDataContext.Policy policy,
            ILogger log)
        {
            // Saving the policy to Azure SQL DB
            var ctx = await WhatIfDemoDbDataContext.CreateAsync();

            ctx.Policies.Add(policy);

            await ctx.SaveChangesIdempotentlyAsync(ex => {
                // Explicitly handling the case of duplicated execution.
                // Which might happen, if the process crashes or restarts.
                log.LogError($"Failed to add policy {policy.id} from userId {policy.userId}: {ex.Message}");
            });

            log.LogWarning($"Saved policy {policy.id} from userId {policy.userId}");

            // Returning the Policy object back, just to show this context propagation mechanism
            return(policy);
        }
        public static async Task ProcessOrder(
            [ServiceBusTrigger("Orders", Connection = "ServiceBusConnection")]
            OrderMessage order,
            [OrchestrationClient]
            DurableOrchestrationClient orchestrationClient,
            ILogger log)
        {
            log.LogWarning($"Processing order from userId {order.userId}");

            // Here is where we instantiate a new Policy object
            var policy = new WhatIfDemoDbDataContext.Policy
            {
                id            = Guid.NewGuid(),
                userId        = order.userId,
                productId     = order.productId,
                paymentAmount = order.price,
                dateCreated   = DateTime.UtcNow
            };

            // Starting the policy processing Saga
            await orchestrationClient.StartNewAsync(nameof(ProcessOrderOrchestrator), policy.id.ToString(), policy);
        }