Chapter 3: Azure Durable Functions
Activity 3: Using a Durable Function to Manage an Email Verification Workflow
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
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:
Turn this into an orchestrator trigger that triggers an orchestrator called OrchestrateVerifyUserEmailWorkflow by adding another argument of type DurableOrchestrationClientBase:
[OrchestrationClient] DurableOrchestrationClientBase orchestrationClientBase,
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):
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:
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:
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:
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: