Go serverless with Azure Functions

It's been over a year since this article was last updated. The information below might be outdated. Please contact the author for more.

I’m very excited that I’m relaunching my blog years later. And in this first post, I’d like to talk about the release of the first preview of Visual Studio 2017 Tools for Azure Functions.

Beyond bringing support for Visual Studio 2017, this release enables creating pre-compiled C# functions and uses WebJobs attributes to declare function bindings directly in the C# code. For me, these features are warmly welcomed and give us a better workflow experience.

In order to follow this tutorial you will need:

Note: These tools are previews. So as usual, it is not supported in production environments.

To get started, create a new project, choose File -> New Project, and the Azure Functions project type.

Class libraries will be produced with .NET 4.6.1 in this first preview version. But in the future .NET Standard 2.0 will be used by default. Additionally, you will can create Azure Functions using Visual Basic and F# in a future upgrade, but support is not included in this release.

Figure 1. Creating a new Azure Functions Project.

This template consists of an empty project with two files:

  • host.json this file can be used to configure the function host
  • local.settings.json which stores setting information such as connection strings used for running the function on the development machine.

Note: For all trigger types except HTTP, you need to set the value of AzureWebJobsStorage to a valid Azure Storage account connection string.

To add a function, just right click the project and choose Add Item, then choose the Azure Function item template.

Figure 2. Choosing the Azure Function item template.

This will open the Azure Function dialog box that lets you choose the type of function you want. For this context we’ll choose the HttpTrigger type, but feel free to try the other types as well.

Figure 3. Choosing the type of the Function.

This generates a new class that has the following elements:

  • A static Run method, that is attributed with [FunctionName] attribute which indicates that the method is the entry for an Azure Function.
  • The first parameter has a HttpTrigger attribute, this is what indicates it is a HTTP trigger function that accepts input through either a query string parameter or the request body.

Once you have a function, local development works like you would expect. You can run and debug it locally, add NuGet packages, create unit tests, and anything else you would do for a class library.

Here’s the default code that this preview version of the tool is generating for you:

Listing 1. Default template code generated.

At this point we can already do our first test. To do this, set a breakpoint somewhere and press F5. When you hit F5 the Visual Studio will ask you to install the Azure Functions CLI tools if they are not already installed.

Figure 4. Installing the Azure Functions CLI tools.

You also can install CLI manually if you want through npm by typing:

Listing 2. Install CLI manually.

Functions in debug mode only run through port 7071. If any of our running applications are already using this port, this may give us some headache.

The endpoint of the Function is always http://localhost:7071/api/{FunctionName}, which is the same format as the actual Azure Functions in the cloud. We can’t change it.

Now it’s waiting for the request coming in. It’s basically an HTTP request, we can send the request through our web browser, Postman or even curl.

This is the request that I made on Postman:

Figure 5. HTTP request with Postman.

When the request above is executed, it will hit the break point within Visual Studio. Super cool! Isn’t it?

App Logic

Now we can enter the logic of the application. For teaching purposes, let’s imagine that we are developing a feature for a mobile application that makes a similar filter to Chrome filter to print or save specific pages of a website.

Figure 6. Page filter.

To get started, double click on the name variable (line 19) to select the whole word. Now press F2 and change it to pages including comments and strings. Don’t forget to change that property of that dynamic variable as well.

Figure 7. Changing the variable “name”.

I don’t intend to explain each part of the changed code to not much extend this tutorial. But when we are ready, the entire code will be similar to this one below.

Listing 3. Final source code.

While this code takes some advantage of the new C# 7 features in a functional approach, it does not care about page order entered or regressive ranges (e.g. 15-10). So be careful if you even want to do something similar in production.

Now hit F5 on Visual Studio, open the Postman and set the endpoint to something similar to: http://localhost:7071/api/HttpTriggerCSharp?pages=1,e,5,,,,10-15

Figure 8. Final result.

It was great to write this first post for my new blog. I hope you enjoyed. We briefly go over how to debug Azure functions in Visual Studio locally. The pre-compiled C# functions bring better cold start performance than script-based functions, and open the entire ecosystem of Visual Studio tools for class libraries including code analysis, unit testing, IntelliSense complete, third-party extensions, etc. So if you were hesitant to use Azure Functions due to lack of features, now is the time to play around it!

Last edited on Jun, 29, 2017 at 7:57 pm