Advanced Scenarios in Azure Functions

Writing Durable Functions

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video segment demonstrates writing and running an example Durable function.


  • Azure
  • Azure Functions
  • Durable Functions
  • Visual Studio

About this video

Sahil Malik
First online
20 December 2019
Online ISBN
Copyright information
© Sahil Malik 2019

Video Transcript

Sahil Malik: With all that background behind us, now let’s see how to write a Durable Function. At the time of recording this course, support for Durable Functions exists in Visual Studio Professional and Enterprise edition, Community edition, et cetera. In other words, you have to use Visual Studio for this, you can’t use Visual Studio Code yet, although I’m sure such support is coming. So go ahead and fire up Visual Studio. And you can search for links on how to set up your Visual Studio environment for developing for Azure Functions but I have already provided you with the links. As long as you have the latest tools installed, you should be ready to go. So let’s go ahead and create a new Azure Functions project.

So I’m going to go to file, new project, and choose to create an Azure Function. I shall put mine in C developer and I’ll call mine FunctionApp and hit okay. Now this is important. Make sure that you pick .NET Core and Azure Functions v2. Now you can target Azure Functions with JavaScript as well, although the tooling for that is still not there. So make sure that you target Azure Functions v2 runtime because Durable Functions don’t work in v1, and pick .NET Core. I’ll go with empty for now. Let’s just go with a local storage emulator and hit okay. So give it a moment for this function app to get created. And I’m sure by the time you’re watching this video, more and more tools and options would have emerged. So take these instructions with a little bit of a grain of salt because by the time you’re watching this, I’m sure JavaScript is supported, VS Code is supported, et cetera.

Okay, here, once this is created, I’m going to say right click, add new Azure Function, and choose to add a new Azure Function. So let me just call mine Function.cs and let’s go ahead and choose to create a Durable Functions orchestration. Hit okay. And this will essentially give you all the code necessary to start writing Azure Functions. It’ll even give you some startup boilerplate code. Let’s go through it but the idea here is that you would take this and now you would enhance it to suit your requirements, all the various design patterns that I talked about. But let’s look at the code that it has given us.

So here you see there is a function called a RunOrchestrator, right. So this is where basically we’re setting up our orchestration and here we have simply said outputs.Add, Function_Hello, Tokyo, Seattle, and London. The Function_Hello is just another function down here as you can see. So I mentioned about this. This is function chaining. So function chaining, essentially what this will do is that this is going to call each function one by one by one. And all of these are going to wait for this entry point function called Function_HttpStart. So what’s happening here in Function_HttpStart is that you’ll be given a URL that you can call over get or post. And essentially this function orchestration is going to wait. It’s going to wait until it receives a get or post and a defined URL So basically it will say “Started orchestration with ID blah” and until it doesn’t get this HTTP start, the whole function orchestration doesn’t start. So it’s basically going to wait for an input.

Now remember that you can implement this in plain Azure Functions as well, but the important point here is that behind the scenes, it’ll provision some sort of storage and it’ll basically create an entry in that storage. This is completely blind to you. You don’t have to write this code. But behind the scenes, that’s what it is doing. It is going to basically write in a little item in that storage and until you get this Function_HttpStart, that storage is basically left alone. And when Function_HttpStart is called, then an entry is made into that storage which starts off the Function 1 Hello for Tokyo and then when Function 1 Hello Tokyo is done, then Function_Hello Seattle is called and then Function_Hello London is called.

So you see here with this little bit of code using await and async and it’s a little bit easier to read because you’re just writing these lines sequentially, you’re able to sort of sequence these operations together and run this orchestration and you’re not paying for a function, let’s say, constantly checking for that item to appear in storage and so on and so forth. So to run this in Visual Studio, you simply hit F5. Now I’m going to show this running in Visual Studio but you can just go ahead and deploy this as well to a function app. Just make sure that your runtime is v2 function environment running .NET Core. So as long as you do that, you can just go ahead and deploy it. And you’ll be able to [refer] [00:05:24] the same in the cloud as well.

So here we go. This starts up. Let’s give it a second. So here we go. It says, host configuration file read, version 1, that’s great. Initialized, give it a moment. Here we go, host started. Okay, here we go. So it says application started and it tells you that there is one function available for me that is listening on get and post, HTTP trigger. So I can call this on get or post, so I’m just going to open the browser and I am going to make a get call here. But I’m going to make this browser really small because the interesting stuff is happening right here. So until I call this, this thing will wait. Now I’m going to hit enter, so now it received a request and now it’ll start calling that function chain one by one.

So let’s look through here. So you see here that as soon as I call this, so this function was programmatically called via the host API. That’s great, that’s good. Let’s go down here. Let’s look through here, Function 1, Tokyo, so it’s saying hello to Tokyo. And then Function_Hello, saying hello to Seattle. And last one should be London. Here you go. Function_Hello London. So you see these execute in a sequence and now you can enhance this logic further. You can make it far more compelling. All the design patterns that I talked about, fan in, fan out, monitoring, waiting for an HTTP request, all of those can be implemented here. And you can deploy this in the cloud and repeat this demo in the cloud very easily as well.