Introduction
With all the talk about Serverless and Azure Functions, it’s a good time to learn how to do this if you don’t already know. In this post, we’ll do a walkthrough of you to create a simple .NET Core 2.1 Azure Function with Visual Studio 2017.
Prerequisites
First of all, let’s set up our environment.
1) Install Visual Studio 2017 (you can download the Visual Studio Community Edition for free here).
2) Install the .NET desktop development workload for Visual Studio. I have these options enabled:
3) Install the Universal Windows Platform development workload:
4) Install the ASP.NET and web development workload:
5) Install the Azure development workload:
6) Install the .NET Core cross-platform development
7) Install the Azure Functions Core Tools:
You can find more info here.
8) Install PostMan. This is a free tool for making HTTP requests so it’s great for testing out APIs. You can download it here.
Create a new Function App V2 project with Visual Studio 2017:
1) Create a new project in Visual Studio (File > New > Project)
2) Select Cloud > Azure Functions then select a folder and click OK.
3) On the new window that pops up, make sure you click the drop down and select Azure Functions v2 (.NET Core) and Http Trigger.
Click OK when done.
4) Let’s add some code. Rename Function1.cs to HttpTrigger.cs and add the contents below.
This method simply takes an HttpRequest input, extracts the two strings FirstName and LastName and outputs a greeting message.
Debug your Azure Function in Visual Studio
1) At the time of writing, there’s an issue with Visual Studio debugging for .NET Core Azure Functions V2. The workaround for this is here. Go ahead and go through this article then come back to continue at step 2.
2) Hit F5 to start debugging your function and you should be presented with something like this
As we can see, our function is running and the HttpTrigger is listed in green.
3) Let’s open up PostMan, create a new Request. It prompts you to create a request name and category.
4) Change the request to POST
5) Go to Body, select raw and add the JSON content below
6) In the console window, copy the HttpTrigger URL into PostMan:
7) Click Send in PostMan
You should get the greeting message below in PostMan
And you should see a request come through in the Azure Function console window
So, there you have it. Your first Azure Function running .NET Core 2.1. Hit Shift+F5 to stop debugging. You can find the project code on GitHub here.