Microsoft is coming up with various changes to its Azure platform. In the past few updates, it has made easy for the developers to create and develop azure functions using visual studio.
In this blog, we will see how to develop and debug azure functions in visual studio.
Before we start developing, we would need the following tools:
- Visual Studio 2017
- Postman or any other tool used for calling REST(for HTTP triggers).
Please make sure that you have latest “Azure Functions and Web Jobs Tools” installed with Visual Studio 2017.
You can verify that by going to Tools>Extension and Updates in Visual Studio. Check if there are any updates to Azure Functions and Web Jobs Tools.
Let start by creating a new solution and project. Go to File>New>Project and select Cloud from the category and then select Azure Functions.
In the next window, you will be asked to select the version of Azure Function you want to create. Azure Function v1 uses .NET Framework and v2 uses .NET Code Framework.
Please note that v2 is currently in preview.
Next, we need to select the type of trigger we want. Select empty if you want to create Azure function later. For this example, I am selecting HTTP trigger.
Those whose function would need data storage can select the appropriate option from the storage account. For now, we can select None.
Access Rights defines the Authorization level for your azure function. Select Anonymous if you want no authorization for your function.
Once done with the selection, click OK to create the project and azure function.
Once the project is created, you can find a file in solution explorer with name Function1.cs.
If you want, you can change the filename. Please do remember to change class names in the file too.
The Function1.cs file will look like below
Run the project by pressing F5 or Debug>Start Debugging. It will open a console window emulator, which will set up the local Azure environment for the azure function.
Once the emulator fully complies, it will display all the azure functions running with the emulator. Since we have only one function, we can see only one function URL.
Open Postman or any other tool for Rest. Use the URL provided in the emulator to trigger the function.
You can pass parameters in a body or as a query string. You need to write logic to parse them.
You can also log messages/error in the emulator by using log object.
Developing Azure function in Visual Studio gives the developer control and OOTB benefits of Visual Studio.
Few of the benefits are:
- The Function can be debugged for errors.
- Various external packages can be added using NPM.
- No need to use #r for referencing and assembly. It can be directly be referenced by “using” keyword.
- The function can directly be published to Azure Resource.