Skip to content

Commit 803aa00

Browse files
authored
Merge pull request #283552 from ggailey777/open-api-updates
[Functions] VS OpenAPI tutorial add isolated worker
2 parents aa2424c + 2e981f1 commit 803aa00

File tree

4 files changed

+96
-30
lines changed

4 files changed

+96
-30
lines changed

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@
380380
"branch": "main",
381381
"branch_mapping": {}
382382
},
383+
{
384+
"path_to_root": "functions-openapi-turbine-repair-isolated",
385+
"url": "https://github.com/Azure-Samples/functions-openapi-turbine-repair-isolated",
386+
"branch": "main",
387+
"branch_mapping": {}
388+
},
383389
{
384390
"path_to_root": "azure-functions-templates-v3",
385391
"url": "https://github.com/Azure/azure-functions-templates",
Binary file not shown.
19.5 KB
Loading

articles/azure-functions/openapi-apim-integrate-visual-studio.md

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,106 @@
11
---
2-
title: Create serverless APIs in Visual Studio using Azure Functions and API Management
3-
description: Use Visual Studio to create an HTTP triggered function along with an OpenAPI definition, which enables Azure API Management integration so that other apps and services can call your serverless function-based API.
2+
title: Create serverless APIs using Azure Functions and API Management
3+
description: Use Visual Studio to create an HTTP triggered function that uses an OpenAPI definition (formerly swagger) to expose a serverless API using API Management.
44
ms.topic: tutorial
5-
ms.date: 01/18/2023
5+
ms.date: 08/04/2024
66
---
77

88
# Create serverless APIs in Visual Studio using Azure Functions and API Management integration
99

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

1212
In this tutorial, you learn how to:
1313

1414
> [!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
1618
> * Test function APIs locally using built-in OpenAPI functionality
17-
> * 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
19+
> * Publish project to a function app in Azure
20+
> * Enable API Management integration
1921
> * Download the OpenAPI definition file
2022
21-
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-
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).
23+
The serverless function you create provides an API that lets you determine whether an emergency repair on a wind turbine is cost-effective. Since you create both the function app and API Management instance in a consumption tier, your cost for completing this tutorial is minimal.
2524

2625
## Prerequisites
2726

2827
+ [Visual Studio 2022](https://azure.microsoft.com/downloads/). Make sure you select the **Azure development** workload during installation.
2928

3029
+ 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.
3130

32-
## Create a Functions project
31+
## Create the code project
3332

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

3635
1. From the Visual Studio menu, select **File** > **New** > **Project**.
3736

3837
1. In **Create a new project**, enter *functions* in the search box, choose the **Azure Functions** template, and then select **Next**.
3938

4039
1. In **Configure your new project**, enter a **Project name** for your project like `TurbineRepair`, and then select **Create**.
4140

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:
42+
43+
### [Isolated worker model](#tab/isolated-process)
44+
45+
**.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).
46+
47+
### [In-process](#tab/in-process)
48+
49+
[!INCLUDE [functions-in-process-model-retirement-note](../../includes/functions-in-process-model-retirement-note.md)]
50+
51+
**.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:
4356

4457
| Setting | Value | Description |
4558
| ------------ | ------- |----------------------------------------- |
46-
| **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. |
4860
| **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. |
4961
| **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 discoverability 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:
74+
75+
### [Isolated worker model](#tab/isolated-process)
5076

51-
![Azure Functions project settings](./media/openapi-apim-integrate-vs/functions-project-settings.png)
77+
```command
78+
NuGet\Install-Package Microsoft.Azure.Functions.Worker.Extensions.OpenApi -Version 1.5.1
79+
```
80+
You might need to update the [specific version](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.Extensions.OpenApi), based on your version of .NET.
81+
82+
### [In-process model](#tab/in-process)
83+
84+
```command
85+
NuGet\Install-Package Microsoft.Azure.WebJobs.Extensions.OpenApi -Version 1.5.1
86+
```
87+
You might need to update the [specific version](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.OpenApi), based on your version of .NET.
88+
89+
---
90+
91+
Now, you can add your HTTP endpoint function.
92+
93+
## Add an HTTP endpoint function
94+
95+
In a C# class library, the bindings used by the function are defined by applying attributes in the code. To create a function with an HTTP trigger:
96+
97+
1. In **Solution Explorer**, right-click your project node and select **Add** > **New Azure Function**.
5298

53-
1. Select **Create** to create the function project and HTTP trigger function, with support for OpenAPI.
99+
1. Enter **Turbine.cs** for the class, and then select **Add**.
54100

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.
101+
1. Choose the **Http trigger** template, set **Authorization level** to **Function**, and then select **Add**. A Turbine.cs code file is added to your project that defines a new function endpoint with an HTTP trigger.
102+
103+
Now you can replace the HTTP trigger template code with code that implements the Turbine function endpoint, along with attributes that use OpenAPI to define endpoint.
56104

57105
## Update the function code
58106

@@ -65,10 +113,18 @@ The function uses an HTTP trigger that takes two parameters:
65113

66114
The function then 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.
67115

68-
In the Turbine.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:
117+
118+
### [Isolated worker model](#tab/isolated-process)
119+
120+
:::code language="csharp" source="~/functions-openapi-turbine-repair-isolated/TurbineRepair/Turbine.cs":::
121+
122+
### [In-process model](#tab/in-process)
69123

70124
:::code language="csharp" source="~/functions-openapi-turbine-repair/TurbineRepair/Turbine.cs":::
71125

126+
---
127+
72128
This function code 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.
73129

74130
## Run and verify the API locally
@@ -127,39 +183,43 @@ Before you can publish your project, you must have a function app in your Azure
127183
| **API name** | TurbineRepair | Name for the API. |
128184
| **Subscription name** | Your subscription | The Azure subscription to use. Accept this subscription or select a new one from the drop-down list. |
129185
| **Resource group** | Name of your resource group | Select the same resource group as your function app from the drop-down list. |
130-
| **API Management service** | New instance | Select **New** to create a new API Management instance in the serverless tier. |
186+
| **API Management service** | New instance | Select **New** to create a new API Management instance in the same location in the serverless tier. Select **OK** to create the instance. |
131187
132188
:::image type="content" source="media/openapi-apim-integrate-vs/create-api-management-api.png" alt-text="Create API Management instance with API":::
133189
134190
1. Select **Create** to create the API Management instance with the TurbineRepair API from the function integration.
135191
136-
1. select **Finish**, verify the Publish page says **Ready to publish**, and then select **Publish** to deploy the package containing your project files to your new function app in Azure.
192+
1. Select **Finish** and after the publish profile creation process completes, select **Close**.
193+
194+
1. Verify the Publish page now says **Ready to publish**, and then select **Publish** to deploy the package containing your project files to your new function app in Azure.
137195
138196
After the deployment completes, the root URL of the function app in Azure is shown in the **Publish** tab.
139197
140198
## Get the function access key
141199
142200
1. In the **Publish** tab, select the ellipses (**...**) next to **Hosting** and select **Open in Azure portal**. The function app you created is opened in the Azure portal in your default browser.
143201
144-
1. Under **Functions**, select **Functions** > **TurbineRepair** then select **Function keys**.
202+
1. Under **Functions** on the **Overview page**, select > **Turbine** then select **Function keys**.
145203
146204
:::image type="content" source="media/openapi-apim-integrate-vs/get-function-keys.png" alt-text="Get an access key for the TurbineRepair function":::
147205
148-
1. Under **Function keys**, select **default** and copy the **value**. You can now set this key in API Management so that it can access the function endpoint.
206+
1. Under **Function keys**, select the *copy to clipboard* icon next to the **default** key. You can now set this key you copied in API Management so that it can access the function endpoint.
149207
150208
## Configure API Management
151209
152-
1. In the **Publish** tab, select the ellipses (**...**) next to **Hosting** and select **Open API in Azure portal**. The API Management instance you created is opened in the Azure portal in your default browser. This API Management instance is already linked to your function app.
210+
1. In the function app page, expand **API** and select **API Management**.
153211
154-
1. Under **APIs**, select **OpenAPI Document on Azure Functions** > **POST Run**, then under **Inbound processing** select **Add policy**.
212+
1. If the function app isn't already connected to the new API Management instance, select it under **API Management**, select **API** > **OpenAPI Document on Azure Functions**, make sure **Import functions** is checked, and select **Link API**. Make sure that only **TurbineRepair** is selected for import and then **Select**.
155213

156-
:::image type="content" source="media/openapi-apim-integrate-vs/apim-add-policy.png" alt-text="Add an inbound policy to the API":::
214+
1. Select **Go to API Management** at the top of the page, and in the API Management instance, expand **APIs**.
215+
216+
1. Under **APIs** > **All APIs**, select **OpenAPI Document on Azure Functions** > **POST Run**, then under **Inbound processing** select **Add policy** > **Set query parameters**.
157217

158218
1. Below **Inbound processing**, in **Set query parameters**, type `code` for **Name**, select **+Value**, paste in the copied function key, and select **Save**. API Management includes the function key when it passes calls through to the function endpoint.
159219

160220
:::image type="content" source="media/openapi-apim-integrate-vs/inbound-processing-rule.png" alt-text="Provide Function credentials to the API inbound processing rule":::
161221

162-
Now that the function key is set, you can call the API to verify that it works when hosted in Azure.
222+
Now that the function key is set, you can call the `turbine` API endpoint to verify that it works when hosted in Azure.
163223

164224
## Verify the API in Azure
165225

@@ -180,7 +240,7 @@ Now that the function key is set, you can call the API to verify that it works w
180240

181241
## Download the OpenAPI definition
182242

183-
If your API works as expected, you can download the OpenAPI definition.
243+
If your API works as expected, you can download the OpenAPI definition for the new hosted APIs from API Management.
184244

185245
1. 1. Under **APIs**, select **OpenAPI Document on Azure Functions**, select the ellipses (**...**), and select **Export**.
186246

@@ -200,7 +260,7 @@ Select **Delete resource group**, type the name of your group in the text box to
200260
201261
## Next steps
202262
203-
You've used Visual Studio 2022 to create a function that is self-documenting because of the [OpenAPI Extension](https://github.com/Azure/azure-functions-openapi-extension) and integrated with API Management. You can now refine the definition in API Management in the portal. You can also [learn more about API Management](../api-management/api-management-key-concepts.md).
263+
You've used Visual Studio 2022 to create a function that's self-documenting because of the [OpenAPI Extension](https://github.com/Azure/azure-functions-openapi-extension) and integrated with API Management. You can now refine the definition in API Management in the portal. You can also [learn more about API Management](../api-management/api-management-key-concepts.md).
204264

205265
> [!div class="nextstepaction"]
206266
> [Edit the OpenAPI definition in API Management](../api-management/edit-api.md)

0 commit comments

Comments
 (0)