Developing Azure Functions with Visual Studio
Since 2002, Visual Studio has been the most important IDE for developing solutions with .NET Framework and it is an important tool to use to implement and test your Azure Functions.
In this book, we will always refer to Visual Studio 2019 but most of the operations we will perform on that version can be done on Visual Studio 2017 (Visual Studio 2017 is the oldest version of Visual Studio that supports Azure Functions development).
Visual Studio 2019 allows you to create an Azure Functions project:
- Simply choose the File | New | Project menu:
- This menu option opens the Visual Studio project type window where you can choose the Azure Functions project type:
If you don't have the Azure Functions template in the project types list, you probably didn't install the Azure development workload. In this case, you can use the Visual Studio Installer tool to install it.
- After you select the project template for Azure Functions (the one selected in the previous screenshot), press the Next button and choose your project and solution name. You can consider the project as the function app in which you put your functions:
- Finally, you choose the function runtime, trigger type, and configuration for the trigger:
As you can see in the previous screenshot, you can also create Azure Functions for runtime version v1.x. Depending on the trigger you choose, the right-hand part of the window shows the configuration values you must insert for the specific trigger. For example, if you choose a Time trigger (as shown in the previous screenshot), the only configuration parameter is the Schedule value.
In this step, you also select (using the Storage Account combo box) when data will be saved (if a trigger works on data such as a queue or blob). You can choose Storage Emulator to use the local storage and test the function locally (without an internet connection).
The value selected in the combo box affects the value of the AzureWebJobsStorage key contained in the local.settings.json file in the project. In the previous case, choosing the Storage Emulator option, you get the following:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
You can, of course, change this value at any time. Visual Studio uses the local.settings.json file only and exclusively for executing code in the local environment. For this reason, you can add sensitive values (for example, access keys to external services) in this file without taking the risk of sharing secrets with other people. The file is, by default, excluded from the source code repository. In fact, Visual Studio creates a .gitignore file and sets local.settings.json as a file to exclude:
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# Azure Functions localsettings file
local.settings.json
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
.
.
.
Once the selected trigger configuration is complete, you can complete the process by pressing the OK button. Visual Studio creates the solution and the project and, inside the project, the Azure Function.
To run your function, you can just press the F5 key: Visual Studio, as usual, starts the restore package operation, then builds the solution, and then starts the Azure Functions runtime (in the same way as you saw in the previous paragraph).
You can put breakpoints in the code in the way you do with the other project types in Visual Studio and intercept the function execution.