Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced Serverless Architectures with Microsoft Azure

You're reading from   Advanced Serverless Architectures with Microsoft Azure Design complex serverless systems quickly with the scalability and benefits of Azure

Arrow left icon
Product type Paperback
Published in Feb 2019
Publisher Packt
ISBN-13 9781788479127
Length 278 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Daniel Bass Daniel Bass
Author Profile Icon Daniel Bass
Daniel Bass
Arrow right icon
View More author details
Toc

Chapter 3: Azure Durable Functions


Activity 3: Using a Durable Function to Manage an Email Verification Workflow

  1. Create a new function app called VerifyUserEmail in a new folder and install Durable Functions, Cosmos DB, and SendGrid:

    func extensions install -p Microsoft.Azure.WebJobs.Extensions.DurableTask -v 1.6.2
    dotnet add package Microsoft.Azure.WebJobs.Extensions.SendGrid --version 3.0.0
    dotnet add package Microsoft.Azure.WebJobs.Extensions.CosmosDB --version 3.0.2

    Figure 3.58: Durable Functions project

  2. Add a function called UserAdded using the CosmosDBTrigger. It will all be templated out by VS Code, but the code is here, too:

    using System.Collections.Generic;
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    using Microsoft.Extensions.Logging;
    
    namespace VerifyUserEmail.OrchestrationTriggers
    {
      public static class UserAdded
      {
        [FunctionName("UserAdded")]
        public static void Run([CosmosDBTrigger(
          databaseName: "serverless",
          collectionName: "users",
          ConnectionStringSetting = "AzureWebJobsStorage",
          LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
        {
          if (input != null && input.Count > 0)
          {
            log.LogInformation("Documents modified " + input.Count);
            log.LogInformation("First document Id " + input[0].Id);
          }
        }
      }
    }

    Your function should look as follows:

    Figure 3.59: Durable Functions project

    Turn this into an orchestrator trigger that triggers an orchestrator called OrchestrateVerifyUserEmailWorkflow by adding another argument of type DurableOrchestrationClientBase:

    [OrchestrationClient] DurableOrchestrationClientBase orchestrationClientBase,

    Figure 3.60: Durable Functions project

  3. Add the orchestrator called OrchestrateVerifyUserEmailWorkflow that triggers an activity called SendUserEmailVerificationRequest to send an email to the user's email address with a link for them to click on (use exactly the same pattern that we used in Exercise 11, Error Notifications with Durable Functions, again):

    Figure 3.61: Durable Functions project

    Create a SendUserEmailVerificationRequest activity that sends the user an email with a link to a function called VerifyEmailAddress. The quickest way to test this would be by using localhost, but you can use either localhost or the deployed version of your app. You function should look as follows:

    Figure 3.62: Durable Functions project

  4. Create an HTTP triggered function called VerifyEmailAddress that emits an EmailVerified event upon a GET request to the address that was sent out in the email:

    Figure 3.63: Durable Functions project

  5. Modify the OrchestrateVerifyUserEmailWorkflow orchestrator to wait for either a timer or the EmailVerified event and call either an activity called SendUserSuccessMessage or SendUserFailureMessage, depending on the result. Your orchestrator should now look as follows:

    Figure 3.64: Durable Functions project

  6. Create an Activity called SendUserSuccessMessage and send the user an email with a successful message. Copy and paste the activity and rename it SendUserFailureMessage before changing the message to a failure message:

    Figure 3.65: Durable Functions project

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image