Yesterday we covered how to develop and debug and Azure Function using Visual Studio. Just as with most things in the world of software development, there are more than one way to go about it. Today we will cover developing and debugging Azure Functions using Visual Studio Code and Powershell. Technically, we will be using the Azure Command Line Interface (CLI) to test our Functions but let's not be pedantic to start.

Prerequisites:

For the purposes of this walk through we will use the CLI installed with Python.

Why Visual Studio Code?

As a developer who has used Visual Studio throughout my career I remember asking the same question when a teammate recommended I check out Visual Studio Code. Visual Studio Code (VS Code) provides a lightweight, cross-platform, alternative to using Visual Studio. VS Code provides the same rich experience whether you're on Windows, macOS or Linux. This allows the developer to have the same experience regardless of where they are doing development.

Microsoft built VS Code to compete with Atom. Atom is a very similar in that it is very lightweight, cross-platform and extendable.

I started using VS Code because I was wanted to get more into development using only Powershell and my IDE. The goal was to simply keep my hands on the keyboard as much as possible. I found it very empowering to open Powershell, navigate to the code I was going to edit, write "Code" and then a file to edit and voila! The editor instantly knew the type of file I was working with and gave me appropriate syntax highlighting and recommended extensions based on the type of file.

If you're not quite sold yet read more here about why you should check out VS Code.

Create and Test a New Function

First, open a Powershell Administrator Window. Then navigate to the location we created in yesterday's demo Azure Functions: Develop and Debug Using Visual Studio. For me this location is "C:\repos\Blog\keithblogsamplefunction" so we can simply run the Powershell command "Set-Location "C:\repos\Blog\keithblogsamplefunction\FunctionApp1"". Once we are there we can write "code ." which will launch VS Code and open the folder for us.

Next, let's go back to Powershell and create a new Function. Write "func new" and you will go through the YeoMan dialogue for creating a new Function. For the new Trigger I chose C# for the language and I created a new HttpTrigger (this is essentially the same as our Hello World example, I promise more complexity is coming soon). Next, we give our new Function a name. For this example I'm going to call it HelloWorldTrigger.

Now, we can switch back over to VS Code. Notice that the new Function automatically showed up for you. Let's open the folder "HelloWorldTrigger" and then open the run.csx file inside. Update line 20 so we can return a different message to the client. We'll make this one say "Hello %name% from Visual Studio Code!"

Update to line 20 in run.csx
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name + " from Visual Studio Code!");

Now we can test our new "Hello World" Function.

In Powershell write "func run." This will launch the local Function Host that we saw yesterday. Copy the URL of your Function to a text editor. It should be something like "http://localhost:7071/api/HelloWorldTrigger".

Next we can send requests using Postman. I covered how to debug your Function yesterday using Postman and the steps are the same except you will use your local URL (the one we copied to the text editor) to send requests to.

The excellent thing with Visual Studio Code is that we can even update our Function in VS Code, save it and see the update returned on our next request. Let's test this out by editing our Hello message again:

Update to line 20 in run.csx
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name + " from Visual Studio Code (with updated text)!");

If you send in a new request from Postman you should see something similar to this in the response: "Hello Keith Hodo from Visual Studio Code (with updated text)!"

Debugging Functions using VS Code

As is my theme, I decided not to reinvent the wheel here. There is a great blog post by Donna Malayeri about debugging an Azure Function using VS Code. Donna's post covers creating and debugging a Javascript function. I couldn't quite figure out how to attach the debugger for C# so until I iron that out I don't have anything to add yet.

Conclusion

Our Webhook still doesn't do a whole lot yet but now it has a friend! In our past two demos we have gone from hosting our Hello World Function on Azure App Service to being able to develop it and debug locally. Additionally, we have seen how to develop and test Azure Functions in a lightweight, cross-platform friendly way using VS Code and Powershell.

What's next: