You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-functions/openapi-apim-integrate-visual-studio.md
+74-15Lines changed: 74 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,52 +7,100 @@ ms.date: 01/18/2023
7
7
8
8
# Create serverless APIs in Visual Studio using Azure Functions and API Management integration
9
9
10
-
REST APIs are often described using an OpenAPI definition. This file contains information about operations in an API and how the request and response data for the API should be structured.
10
+
REST APIs are often described using an OpenAPI definition (formerly known as Swagger) file. This file contains information about operations in an API and how the request and response data for the API should be structured.
11
11
12
12
In this tutorial, you learn how to:
13
13
14
14
> [!div class="checklist"]
15
-
> * Create a serverless function project in Visual Studio
15
+
> * Create the code project in Visual Studio
16
+
> * Install the OpenAPI extension
17
+
> * Add an HTTP trigger endpoint, which includes OpenAPI definitions
16
18
> * Test function APIs locally using built-in OpenAPI functionality
17
19
> * Publish project to a function app in Azure with API Management integration
18
-
> * Get the access key for the function and set it in API Management
20
+
> * Get the access key for the function app and set it in API Management
19
21
> * Download the OpenAPI definition file
20
22
21
23
The serverless function you create provides an API that lets you determine whether an emergency repair on a wind turbine is cost-effective. Because both the function app and API Management instance you create use consumption plans, your cost for completing this tutorial is minimal.
22
24
23
-
> [!NOTE]
24
-
> The OpenAPI and API Management integration featured in this article is currently only supported for [in-process](functions-dotnet-class-library.md) C# class library functions. [Isolated worker process](dotnet-isolated-process-guide.md) C# class library functions and all other language runtimes should instead [use Azure API Management integration from the portal](functions-openapi-definition.md).
25
-
26
25
## Prerequisites
27
26
28
27
+[Visual Studio 2022](https://azure.microsoft.com/downloads/). Make sure you select the **Azure development** workload during installation.
29
28
30
29
+ An active [Azure subscription](../guides/developer/azure-developer-guide.md#understanding-accounts-subscriptions-and-billing), create a [free account](https://azure.microsoft.com/free/dotnet/) before you begin.
31
30
32
-
## Create a Functions project
31
+
## Create the code project
33
32
34
-
The Azure Functions project template in Visual Studio creates a project that you can publish to a function app in Azure. You'll also create an HTTP triggered function supports OpenAPI definition file (formerly Swagger file) generation.
33
+
The Azure Functions project template in Visual Studio creates a project that you can publish to a function app in Azure. You'll also create an HTTP triggered function from a template that supports OpenAPI definition file (formerly Swagger file) generation.
35
34
36
35
1. From the Visual Studio menu, select **File** > **New** > **Project**.
37
36
38
37
1. In **Create a new project**, enter *functions* in the search box, choose the **Azure Functions** template, and then select **Next**.
39
38
40
39
1. In **Configure your new project**, enter a **Project name** for your project like `TurbineRepair`, and then select **Create**.
41
40
42
-
1. For the **Create a new Azure Functions application** settings, use the values in the following table:
41
+
1. For the **Create a new Azure Functions application** settings, select one of these options for **Functions worker**, where the option you choose depends on your chosen process model:
**.NET 8.0 Isolated (Long Term Support)**: Your C# functions run in the isolated worker model, which is recommended. For more information, see the [isolated worker model guide](dotnet-isolated-process-guide.md).
**.NET 8.0 (Long Term Support)**: Your C# functions run in the [in-process model](functions-dotnet-class-library.md), which will be retired. To learn more, see the [in-process model guide](functions-dotnet-class-library.md).
52
+
53
+
---
54
+
55
+
1. For the rest of the options, use the values in the following table:
|**Functions worker**|**.NET 6**| This value creates a function project that runs in-process on version 4.x of the Azure Functions runtime, which is required for OpenAPI file generation. |
47
-
|**Function template**|**HTTP trigger with OpenAPI**| This value creates a function triggered by an HTTP request, with the ability to generate an OpenAPI definition file. |
59
+
|**Function template**|**Empty**| This creates a project without a trigger, which gives you more control over the name of the HTTP triggered function when you add it later. |
48
60
|**Use Azurite for runtime storage account (AzureWebJobsStorage)**|**Selected**| You can use the emulator for local development of HTTP trigger functions. Because a function app in Azure requires a storage account, one is assigned or created when you publish your project to Azure. |
49
61
|**Authorization level**|**Function**| When running in Azure, clients must provide a key when accessing the endpoint. For more information, see [Authorization level](functions-bindings-http-webhook-trigger.md#http-auth). |
62
+
63
+
1. Select **Create** to create the function project.
64
+
65
+
Next, you update the project by installing the OpenAPI extension for Azure Functions, which enables the discoverabilty of API endpoints in your app.
66
+
67
+
## Install the OpenAPI extension
68
+
69
+
To install the OpenAPI extension:
70
+
71
+
1. From the **Tools** menu, select **NuGet Package Manager** > **Package Manager Console**.
72
+
73
+
1. In the console, run the following [Install-Package](/nuget/tools/ps-ref-install-package) command to install the OpenAPI extension:
You might need to updated the [specific version](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.OpenApi), based on your version of .NET.
You might need to updated the [specific version](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.OpenApi), based on your version of .NET.
88
+
89
+
---
90
+
91
+
Next, you can add create your HTTP endpoint function.
92
+
93
+
## Add an HTTP endpoint function
94
+
95
+
In a C# class library, the bindings used by the functionare defined by applying attributes in the code. To create a functionwith an HTTP trigger:
52
96
53
-
1.Select**Create** to create the function project and HTTP trigger function, with support for OpenAPI.
97
+
1. In**Solution Explorer**, right-click your project node and select**Add**>**New Azure Function**.
54
98
55
-
Visual Studio creates a project and class named `Function1` that contains boilerplate code for the HTTP trigger function type. Next, you replace this function template code with your own customized code.
99
+
1. Enter **Turbine.cs**for the class, and thenselect**Add**.
100
+
101
+
1. Choose the **Http trigger** template, set**Authorization level** to **Function**, and thenselect**Add**. A Turbine.cs code file is added to your project that defines a new functionendpoint with an HTTP trigger.
102
+
103
+
Now you can replace the HTTP trigger template code with code that implements the Turbine functionendpoint, along with attributes that use OpenAPI to define endpoint.
56
104
57
105
## Update the function code
58
106
@@ -65,10 +113,21 @@ The function uses an HTTP trigger that takes two parameters:
65
113
66
114
The functionthen calculates how much a repair costs, and how much revenue the turbine could make in a 24-hour period. Parameters are supplied either in the query string or in the payload of a POST request.
67
115
68
-
In the Function1.cs project file, replace the contents of the generated class library code with the following code:
116
+
In the Turbine.cs project file, replace the contents of the class generated from the HTTP trigger template with the following code, which depends on your process model:
You might need to updated the [specific version](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.OpenApi), based on your version of .NET.
This functioncode returns a message of `Yes` or `No` to indicate whether an emergency repair is cost-effective. It also returns the revenue opportunity that the turbine represents and the cost to fix the turbine.
0 commit comments