Azure Functions & Transient Exceptions

Call other functions inside Azure Functions

This post covers how to deal with transient issues within Azure Functions. The process is simple, thanks to available helper libraries. All my code samples are in C#.

Azure Functions and Transient Fault Handling

Dealing with transient fault handling isn’t new. When cloud-based applications use other cloud services, things might go wrong. This is because of temporary conditions such as intermittent service, network issues, etc. In most cases, the operation may succeed when retrying shortly later.

Transient Fault Handling libraries

Writing retry logic isn’t that simple. Coding cancellation and dynamic wait duration support is a lot of work. Fortunately, libraries that can help to deal with Transient errors do exist.

Polly

I am a big Polly fan. It’s easy to use, has many features and supports async operations. Because of this, I won’t cover any other options. Keep in mind that you can use any library of your liking. However, make sure it’s available as a NuGet Package.

azure-functions-polly-1

Building A C# sample function

Let’s get started with creating a simple function. This Function gathers information from an external web service.

Step 1 – Creating a new Function App.

A Function App is a container that hosts Azure Functions. Creating a new Function App using the Portal (or API) is simple. Once created, locate and open the Function App within the Portal.

azure-functions-1

Step 2 – Creating a new Function.

Within the interface select, “New Function”. A new selection screen should display containing pre-defined templates. I’m using a timer-based function to simplify the test case. Please select your desired template, however, make sure it’s using C#.

Within the code section, I’ve included the code, as displayed below. Here I’m calling a web service using rest sharp. The call is surrounded with an exception/retry policy.

Mode about the code

A couple of remarks I would like to make regarding this code sample. First of all, make sure to visit Polly’s GitHub page. Here you can find lots of documented samples. In my sample, I’m creating a simple retry policy. I’ve set the number of retries to 3 with a dynamic wait duration.

I’ve wrapped the policy within a Try Catch block. This, in case recovery isn’t possible. Within the policy execution block, I’m throwing a custom exception. This to force a retry based on fixable status codes.

Note: You don’t have to throw custom exceptions by using HandleResult. You can find additional details in section 1B.

Step 3 – Importing Nuget packages.

As mentioned in step 2, the code uses two Nuget packages. To import packages into your Function, you will need to create a project.json file. You can do this using a tool called Visual Studio Online. To open VS Online, click Function app settings located in the right corner.

Locate Advanced settings at the bottom of the screen. Click on Go to App Service Settings.

azure-functions-3

Within the management blade, find the section called Development Tools. And open VS Online by clicking on App Service Editor, Go.

azure-functions-4

A new screen will open with the layout of your functions. By default, every Function has a run.csx, which contains your function code. Besides also contains a function.js which contains config specific information.

azure-functions-5-1

Within the root level of your Function, create a file called project.json. The example below shows how to reference existing Nuget packages.

Note: Only the .NET Framework 4.6 is supported. Therefore, make sure that your project.json file specifies net46 as shown here.

After this, you can import the namespace as usual, with the using clause.

Step 5 – Testing your new Function.

Testing Transient Faults are hard to test because of their unpredictable nature. You can create a web service mock to see how your code behaves. What’s important is to keep an eye on your Policy and Execution log entries.

Once you have a setup that can mimic server errors, it’s simple to initiate a test run. On the developer tab, locate the button called Run.

azure-functions-polly-example-result-1

Further reading

I’ve included some additional resources below. I hope this was helpful and more on Azure Functions soon!

Call Functions inside Azure Functions
Reusing code within Azure Functions
App-vNext/Polly transient exception and fault handling for .net

Post Navigation