Azure Durable Functions

This blogs talks about how to create a simple Durable Function App.

There are three components to Durable Function App.

  1. Main / Starter Function
  2. Workflow / Orchestrator Function
  3. Activity Function

Durable Function App needs all three functions. Main / Starter function is responsible for starting a workflow instance. Workflow / Orchestrator function has workflow logic, calls activities and waits for external events. Activity function, actual work is implemented here.

In this sample Durable Function App, we are going to create a workflow with three activities and one external event. We are going to start workflow via PostMan. We are also going to use PostMan to get workflow instance status and to fire external events. 

Here are high level steps:

  1. Create Function App
  2. Create Main / Starter Function
  3. Create Workflow Function
  4. Create Activity Function
  5. Start Workflow
  6. Workflow with external events

1. Create Function App

1. Log into Azure Portal: https://portal.azure.com

3. Click on the Add button as shown below

clip_image002

4. In the search box, type Function App and click on the Function App resource as shown below

clip_image004

5. Click on Create button

6. In Function App blade, set name to devbootcampdurable as shown below

clip_image006

7. Click on the Create button

8. Once the resource is created, navigate to this newly created Function App resource

2. Add new Durable Function

1. Open a new tab in the browser and navigate to Azure portal https://portal.azure.com

2. Go to devbootcamp Resource Group

3. Click on the devbootcampdurable{your alias} Function App

4. Hit >> button next to Functions

5. Select Function App Settings as shown below

clip_image008

6. Set the Function Runtime to beta as shown below

clip_image010

7. Now click on the + button next to Functions

8. Select “Custom function” as shown below

clip_image012

9. Change the Scenario to Durable Functions

10. Click on the “Durable Functions HTTP starter” C# Function as shown below

clip_image014

11. Next, you should see prompt to install Durable Extensions as shown below

clip_image016

12. This should take about 2 min to complete. If you don’t see this dialog box, try again: go back to step #7

13. Once the extension is installed, you should see this below success dialog box

clip_image018

14. Next, portal will prompt to create a new Function.

15. Set the name to Main as shown below

clip_image020

16. Review the Main function code.

17. See the functionName variable and instanceId variable

clip_image022


3. Add Workflow Function

1. Click on the + button next to Functions

2. Set the scenario to Durable Functions

3. Select Durable Orchestrator Function as shown below

clip_image024

4. Set the name of the function to WorkflowOne as shown below

clip_image026

5. Click on the Create button

6. Once the WorkflowOne is created, review the code.

7. There are three activity calls to Hello Function show below

clip_image028

8. Change the Function name from Hello to ActivityOne, ActivityTwo, ActivityThree as shown below

clip_image030


4. Add Activity One Function

1. Click on the + button next to Functions

2. Set the scenario to Durable Functions

3. Select Durable Functions activity as shown below

clip_image032

4. Set the name of the function to ActivityOne as shown below

5. Click on the Create button

clip_image034

6. Review the function code

clip_image036

7. Update the code as shown below:

clip_image038

8. Add two more activity functions, just go back to step # 1

clip_image040


5. Start Workflow

1. Click on the Main function

2. Click on the Get Function URL link as shown below

clip_image042

3. Next click on the copy

clip_image044

4. Open PostMan (install it from https://getpostman.com)

5. Copy this URL into PostMan

6. Rename {funcitonName} with WorkflowOne as shown below.

7. Set the HTTP to POST and click on Send

clip_image046

8. Review response. Look for statusQueryGetUri

clip_image048

9. Now click on the statusQueryGetUri URL in the PostMan

10. Add these to end of the URL and click on the SEND as shown below

&showHistory=true&showHistoryOutput=true

clip_image050

11. Review response. Look for output for the workflow and individual activities

clip_image052


6. Wait for External Event

1. Go back to WorkflowOne function

2. Add these lines before return statement as shown

string strApprovalEvent = await context.WaitForExternalEvent<string>("ApprovalEvent");

if(strApprovalEvent == "true")

{

outputs.Add("Approved");

}

else

{

outputs.Add("Rejected");

}

clip_image054

3. Start a new instance of Workflow, by click on the PostMan URL. Follow the step #1 in the previous section

4. Now try to get the status of the Workflow, Follow the step # 9 in the previous section

5. Review the results. Workflow is still running and all three activities are done.

6. Workflow is now waiting for external event as shown below

clip_image056

7. Now, click on the sendEventPostUri URL from the PostMan results for starting workflow

8. Remane the {eventName} with ApprovalEvent as shown below

clip_image058

9. Change the HTTP method to POST

10. Select the Body, select raw and content type to JSON

11. In the body field type “true”

12. Hit Send button as shown below:

clip_image060

13. Now check the status again. Follow the step # 9 in the previous section

14. Review the output. Do you see Approved message ?

clip_image062

15.