Setup Azure Functions development with Visual Studio

Disclaimer: This blog was published before the official tooling for Visual Studio was released, please refer to this blog post for the latest guidance.

Azure Functions is a great new Azure service currently in public preview. It allows you to harness an event-driven, compute-on-demand model without worrying about any underlying webservers or operating systems. In this sense, it aligns very well with the concept of Serverless compute. It's the natural evolution of the Azure App Service's WebJobs and allows you to link code to triggers and input/output bindings. There is already a rich selection of bindings for you to use OOTB, including (but not limited to); DocumentDB, EventHubs, HTTP WebHooks, Timers and Service Bus.

As this service is currently in preview there are a number of rough edges, most of which the engineering team are aware of and trying to iron out before the service becomes GA. If you would like to give any feedback or check on current suggestions, please head over to the feedback portal.

One of these rough edges is currently the developer experience. Azure Functions offers you an in browser text editor to write your functions along with a plethora of sample function templates. However, when it comes to writing your own functions from scratch the experience isn't currently great. One option is to setup 'continuous integration and deployment' from source control and write your code in an offline editor such as Visual Studio Code. However, I feel this still lacks the true richness of an IDE development experience. Therefore, what I really want is to develop my application offline in Visual Studio. There is an article published by the engineering team which steps you through how you can achieve this; https://azure.microsoft.com/en-us/documentation/articles/functions-run-local/. Bear in mind you will still require a Function App and blob storage in Azure as this isn't yet a proper offline development solution.

As the current offline development solution is not quite where I'd like it to be, I'd rather just work in Visual Studio and manually publish to an Azure hosted dev Function App with remote debugging, diagnostic logs etc. If you're like me then follow the below steps to get up and running.

  • Go to http://portal.azure.com and create a new Azure Function App.
  • Using Quickstart (shown below), create a default function i.e. C# Timer
    /content/images/2016/09/quickstart.PNG

  • Select Function App Settings from the lower left hand menu.

  • Select Kudu from the options

Using the on screen file system explorer; navigate to the site folder and then download the wwwroot folder by clicking on the download icon.

Extract the downloaded .zip file into a working directory on your local machine. Open Visual Studio and select File > Open > Web Site... and browse to the working directory. Press OK.
This should open a solution with the default C# Timer function folder inside. Inside the C# Timer function folder should be function.json, run.csx, and a readme.md files. You'll want to make sure the root folder also has a host.json file in it, if it doesn't, read the documentation and create one. It might also be worth initialising the root folder as a git repository and adding a .gitignore (which will exclude some of the generated files that will be created shortly getting into your source control) - Remember a Function App can have many functions. The directory structure we're looking for is similar to this;

wwwroot  
 | - host.json
 | - mynodefunction
 | | - function.json
 | | - index.js
 | | - node_modules
 | | | - ... packages ...
 | | - package.json
 | - mycsharpfunction
 | | - function.json
 | | - run.csx

Next if you right click on the project in Visual Studio, click Publish Web App, fill in the wizard to create a publish profile that links to your Azure Function App and then attempt to publish via the dialogue box.

If that fails (as mine did) with the error: Cannot publish to Azure: 'Can't find existing loaded project' error'

You have to publish through the Web Publish Activity instead. If this windows isn't already loaded in your Visual Studio workspace, select View > Other Windows > Web Publish Activity, once loaded click the small publish icon (the globe with an arrow wrapped around it). With any luck this should successfully publish. Now you can delete the default C# Timer function folder, add your own function folder, with a corresponding function.json, run.csx and readme.md file and build your own functions in Visual Studio and publish directly into your development Azure Function App.
Bare in mind that even the Visual Studio developer experience is slightly hampered, for instance to pull in a NuGet reference in an Azure Function App you use the #r Newtonsoft.Json syntax which Visual Studio doesn't know how to tokenize. For similar reasons you will find you intelliSense prompt is a little less active than it would be in regular C#. Hopefully soon we'll see these niggles get taken care of.