Create a dotnet core Azure Function
written on January 6, 2018
Latest version of Visual Studio Extension Azure Functions and Web tools helps you create pre-compiled C# Azure Functions, and lets you write functions as a C# library. Being a pre-compiled function it has better performance while starting up. Along with that, it lets you use Webjobs attributes for declaring bindings, thus not needing a functions.json file. And, it lets you debug and run azure functions locally.
Moreover, being a C# library, it makes it possible to write unit tests, and use code analysis for Azure Function. In this post we’ll limit ourselves to creating a dotnet core Azure Function, and then later posts will cover unit testing an Azure Function, and how to do CI/CD for Azure Functions.
Creating Azure Function in Visual Studio
- Make sure you’ve installed latest version of Visual Studio 2017 (atleast 15.5.3), and Azure workload is already installed. This would’ve installed base version of the Azure Functions extension. Now download and update the extension Azure Functions and Web tools.
- Above template lets you create Azure Function v2 version which is in dotnet core. You must understand that currently Azure Functions v2 is in preview, and this is not fit for production.
- Create a new project in Visual Studio and select Azure Functions template.
Ignore the first screen for creating Azure Function as it still shows .Net Framework versions. Don’t get confused with that, and be patient, and go to the next step.
At the next step you can select to use Azure Functions v2. The new template lets you select a storage account and access rights. You can select any trigger type based on your need. You can even select empty, and then add a new function to your project later.
I chose Empty function, so as to demonstrate how to add a new function after creating project. Project will be created now, and if you see it’s properties you’ll see its target framework as .NET Standard 2.0.
Right click on your project, and select Add -> New Azure Function. Give your function a name, and click Add.
- At next step, I need a Http Trigger, function, so I selected that, and hit Ok.
- At this point of time, you’ll have a fully functional Azure function. Notice that there’s no function.json file. This will be generated when the function is built. Note that function name is specified with
- The default function created here is a fully functional one. Hit F5 in visual studio, and it will deploy it locally and run it. You can even set a breakpoint to help with debugging.
When you run the project Azure Function runtime will show the endpoints of all the function in your project. For example, in this case, there’s only one function
- The default function created here, supports both GET and POST, and expects
nameproperty. You can either test it from browser by adding a querystring called
namesuch as http://localhost:7071/api/DrivingLicenseValidator?name=aakash, or using POSTMan.
- If you go to your bin\debug folder, you’ll notice that there’s a folder created for your Azure function, which indludes a function.json file. This would be done for each Azure Function in your project. When your function project id built, a subfolder would be created for each function with function.json file. This function.json file has information related to bindings, points to the compiled dll to be used in the
entryPointwhich is different for each function that points to where the Run method is located.