Getting Started with Azure Functions for VS 2017

There was a heap of awesome things announced at Microsoft Build 2017 , Scott Hanselman  has a great roll up of the most important developer announcements here.

One that I have been spending some time getting up to speed on is the Azure Functions Tooling for VS 2017. Here are a few tips I have learnt.

Firstly as of April 2017 the tooling is in preview mode. That means you can’t just add it to a regular installation of Visual Studio , you can however run the preview side by side with the main version assuming you have enough disk space. The options for running Visual Studio Preview are covered well here.

The specific version of Visual Studio and a link to the preview tools for Azure functions are covered in this article as are the steps for creating your first Function.

You need to think carefully about what sort of Function you want to create as there is no wizard or property selection that allows you to easily change to another type. It is possible to do manual updates to change the properties but generally better to select the correct template initially.

The current options are:

  • BlogTrigger
  • EventHubTrigger
  • GenericWebHook
  • GitHubWebHook
  • HttpTrigger
  • HttpTriggerWithParameters
  • QueueTrigger
  • ServiceBusTopicTrigger
  • TimerTrigger
  • FaceLocator
  • GitHubCommenter
  • HttpGetCRUD
  • HttpPostCRUD

The first thing you need to do before you run the function is set the AzureWebJobsStorage key in the local.settings.json file. This is necessary for all functions apart from the http based ones to be able to run locally.

Selecting the correct template gives an easy way to configure the correct input trigger.
Azure functions also has output bindings , all the different options are covered here. however how to do this when using VS 2017 is a little bit different.

Essentially you just add in the the correct output item to the run function and a connection string in local.settings.json so it can run locally.

For example a function that is triggered by timer would initially look like:

public static void Run([TimerTrigger(“0 */5 * * * *”)]TimerInfo myTimer,  TraceWriter log)

To add an output trigger for service bus it would then look like the below :

public static void Run([TimerTrigger(“0 */5 * * * *”)]TimerInfo myTimer, [ServiceBus(“evbsbtest”, AccessRights.Send, Connection = “sbconn”)] ICollector<string> outputSbQueue, TraceWriter log)

This also requires adding a using  statement for Microsoft.ServiceBus.Messaging  for the access rights object.

Another item that I hit was an error when I added a service bus binding starting to run the function  caused this error  “The following 1 functions are in error: Can’t figure out which ctor to call” . After trying in vain to debug it I released that this was actually a bug in the Azure Functions SDK as noted here.

In my project I then clicked manage Nuget packages and sure enough an update was sitting there waiting for me to install.  Given the alpha status of this package it is quite likely it will be frequently updated as issues are found.  Hence well worth checking the open and recently resolve issues if you do hit a problem !

The final tip I have is that as Azure functions are easy to write and new and shiny I find myself very easily adding way too much into a single function. Hence I find I have to constantly re-evaluate whether I am breaking best practice.  In several cases already I have ended up refactoring what I thought would be a single function into multiple and adding in a logic app to assist with orchestration to keep the functions stateless and short running these and other Azure functions best practices can be found here