Skip to content

Latest commit

 

History

History
179 lines (127 loc) · 7.46 KB

azure-functions-v1-support.md

File metadata and controls

179 lines (127 loc) · 7.46 KB

Support Azure Functions v1 with OpenAPI Extension

Due to the backward compatibility issue, this OpenAPI extension has dropped supporting Azure Functions v1 runtime. However, there are many enterprise scenarios that still need Azure Functions v1 runtime. Although there is no direct support from this OpenAPI extension, you can still take the benefits of using this extension, using the Azure Functions Proxy feature.

Prerequisites

To get yourself started, you need to have the followings installed on your local machine.

Important

This extension is currently available in .NET Core runtime.

Create Function App

Firs of all, create a function app on your local machine.

func init MyV1FunctionAppProxy --dotnet

Navigate to the project directory

cd MyV1FunctionAppProxy

Add a function to your project by using the following command, where the --name argument is the same name of your v1 function (MyHttpTrigger) and the --template argument specifies HTTP trigger.

func new --name MyHttpTrigger --template "HTTP trigger"

Inside the function method, replace all code with the following one line of code. It's assumed that the v1 Function has the endpoint of /api/lorem/ipsum. The proxy function MUST have the same endpoint.

namespace MyV1FunctionAppProxy
{
    public static class MyHttpTrigger
    {
        [FunctionName("MyHttpTrigger")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "GET", Route = "lorem/ipsum")] HttpRequest req,
            ILogger log)
        {
            return await Task.FromResult(new OkResult()).ConfigureAwait(false);
        }
    }
}

Add OpenAPI Extension

To enable OpenAPI metadata, you will need to install a NuGet package, Microsoft.Azure.WebJobs.Extensions.OpenApi.

dotnet add package Microsoft.Azure.WebJobs.Extensions.OpenApi

Add attribute classes on top of the FunctionName(...) decorator.

namespace MyV1FunctionAppProxy
{
    public static class MyHttpTrigger
    {
        // Add these three attribute classes below
        [OpenApiOperation(operationId: "lorem", tags: new[] { "name" }, Summary = "Gets lorem ipsum", Description = "This gets lorem ipsum.", Visibility = OpenApiVisibilityType.Important)]
        [OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(string), Summary = "The name", Description = "The name", Visibility = OpenApiVisibilityType.Important)]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Summary = "The response", Description = "This returns the response")]
        // Add these three attribute classes above

        [FunctionName("MyHttpTrigger")]
        public static async Task<IActionResult> Run(
...

By doing so, this Function app is now able to generate the OpenAPI definition document on-the-fly. However, it won't work as expected because there is no business logic implemented.

Add Functions Proxy

As this is the proxy app, you need to implement the proxies.json file at the root directory. <my-v1-function-app> is your Azure Functions v1 app.

{
  "$schema": "http://json.schemastore.org/proxies",
  "proxies": {
    "ProxyLoremIpsum": {
      "matchCondition": {
        "route": "/api/lorem/ipsum",
        "methods": [
          "GET"
        ]
      },
      "backendUri": "https://<my-v1-function-app>.azurewebsites.net/api/lorem/ipsum",
      "requestOverrides": {
        "backend.request.headers": "{request.headers}",
        "backend.request.querystring": "{request.querystring}"
      }
    }
  }
}

You also need to update your MyV1FunctionAppProxy.csproj file to include this proxies.json file during the build time.

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <ItemGroup>
    ...
    <None Update="proxies.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  ...
</Project>

Then all your API requests to the proxy function are forwarded to the v1 Function app and processed there.

Clean-up Resources

When you continue to the another step, Integrating OpenAPI-enabled Azure Functions with Azure API Management, you'll need to keep all your resources in place to build on what you've already done.

Otherwise, you can use the following steps to delete the function app and its related resources to avoid incurring any further costs.

  1. In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select Azure Functions: Open in portal.

  2. Choose your function app, and press Enter. The function app page opens in the Azure portal.

  3. In the Overview tab, select the named link next to Resource group.

    Select the resource group to delete from the function app page

  4. In the Resource group page, review the list of included resources, and verify that they are the ones you want to delete.

  5. Select Delete resource group, and follow the instructions.

    Deletion may take a couple of minutes. When it's done, a notification appears for a few seconds. You can also select the bell icon at the top of the page to view the notification.

To learn more about Functions costs, see Estimating Consumption plan costs.

Next Steps

You have got an Azure Functions app with OpenAPI metadata enabled. In the next articles, you will be able to integrate this OpenAPI-enabled Azure Functions app with either Azure API Management, Azure Logic Apps or Power Platform.