官术网_书友最值得收藏!

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:

  1. Simply choose the File | New | Project menu:
  1. 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.

  1. 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:

  1. 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.

主站蜘蛛池模板: 商水县| 肇源县| 应城市| 邵阳市| 柏乡县| 天柱县| 河北区| 宜城市| 惠东县| 改则县| 东平县| 武鸣县| 德阳市| 房产| 玉屏| 北宁市| 永嘉县| 徐汇区| 松原市| 文安县| 保康县| 石狮市| 巴彦县| 巴彦县| 酉阳| 馆陶县| 曲松县| 天长市| 尼勒克县| 吴江市| 拜泉县| 资溪县| 海淀区| 大田县| 丁青县| 肇东市| 北海市| 定结县| 玛纳斯县| 绩溪县| 乐业县|