Feature support (preview) for Azure FunctionsOpenAPI was officially announced at Build in May. At the time, it supported up to the V3 runtime -.net Core version 3.1. It recently released the.NET 5 Isolation worker support pack as a preview. In this article, I’ll review how to use it and deploy it to Azure.

Note: You can find the sample code used in this article in the GitHub repository: github.com/justinyoo/a…

Create the Azure Functions application in.NET 5

Let’s use Visual Studio to complete this exercise. When creating the application, use the “.NET 5(Isolation)” runtime and “Http triggers “.

You will then find the HTTP endpoint with the default code. Now, on solution Explorer, select the NuGet Package Manager menu.

In the NuGet package manager screen, check the “include pre-release” check box, and then search the Microsoft. Azure. Functions provides. Worker. Extensions. OpenApi package. At the time of this writing, the NuGet package version is V0.81-Preview.

The OpenAPI extension is now installed.

Configuration HostBuilder

After installing the OpenAPI extension, let’s configure HostBuilder. First, open the Program. The cs file and remove the existing ConfigureFunctionsWorkerDefaults () method. This is because the method uses System.text.json by default, and we won’t use it.

 public static void Main(){
        var host = new HostBuilder()
            // 👇👇👇👇👇 delete the following line 👇👇👇👇👇
            .ConfigureFunctionsWorkerDefaults()
            // 👆👆👆👆👆 delete the above line 👆👆👆👆👆
            .Build(); 
        host.Run();
    }
Copy the code

Then, in this order add ConfigureFunctionsWorkerDefaults (worker = & gt; Worker.usenewtonsoftjson ()) and ConfigureOpenApi() methods. The first method explicitly declares to use the newtonsoft.json package, and the next imports additional OpenAPi-related endpoints.

public static void Main() 
{
        var host = new HostBuilder()
            // 👇👇👇👇👇 Add these lines below 👇👇👇👇👇
            .ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
            .ConfigureOpenApi()
           // 👆👆👆👆👆 Add these lines above 👆👆👆👆👆
           .Build();
        host.Run();
}
Copy the code

Note: Currently, using system.text. Json is no guarantee that your application will work properly. Therefore, the use of newtonsoft.json is highly recommended.

The configuration is complete. Let’s move on.

Add OpenAPI modifiers

Add openAPI-related modifiers, as shown below. This is exactly the same as the existing method, so I won’t go into too much depth.

// 👇👇👇👇👇 add the openAPI-related modifier 👇👇👇👇👇 below
    [OpenApiOperation(operationId: "greeting", tags: new[] { "greeting" }, Summary = "Greetings", Description = "This shows a welcome message.", Visibility = OpenApiVisibilityType.Important)]
    [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
    [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Summary = "The response", Description = "This returns the response")]
    // 👆👆👆👆👆 add the OpenAPI-related modifier 👆👆👆👆👆 to it
    [Function("Function1")]
    public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get"."post")] HttpRequestData req,
        FunctionContext executionContext){... }Copy the code

Once you’re done adding modifiers, you’re done! Let’s run the application.

Run the Swagger UI

Enter F5 or clickVisual StudioThe debug button runs the Function application.

You will see the OpenAPi-related endpoints added to the console.

Endpoint in a web browser running on http://localhost:7071/api/swagger/ui, you will see the Swagger UI pages.

The Azure Function application with OpenAPI functionality is now properly implemented.

Deploy the Azure Function App — Windows

You confirm that your Azure Function application is working properly. Now you need to deploy the project. First, click the Publish menu in Solution Explorer.

Select “Azure”, then select “Azure Functions App (Windows)”.

You can use an existing Function application instance or create a new application instance by clicking the :heavy_plus_sign: button. This time, let’s use the current example.

Once deployed, open Azure Functions URL in your Web browser and you’ll see the Swagger UI page.

Deploying Azure Function applications – Linux

This time, let’s deploy the same application to a Linux instance. Beyond that, let’s use GitHub Actions. To do this, you must upload the application to the GitHub repository. Therefore, move to the “Git Changes” pane and create a Git repository.

If you are already inVisual StudioBy logging into GitHub, you can create a repository and push code.

Once all the code has been pushed, visit GitHub to check that your repository has actually uploaded all the code.

Let’s go back to the release screen and click the “:heavy_plus_sign: New” button to create a New release profile.

A similar pop-up window will then appear. Let’s use the “Azure Function App (Linux)” menu this time.

As mentioned earlier, you can use an existing instance or create a new one. Let’s just use what we have.

In the previous deployment exercise, we did not have a GitHub repository. Therefore, we had to use a locally deployed approach. But this time, we have the GitHub repository, which means we have choices. So instead of using the same deployment method this time, we chose GitHub Actions.

The GitHub Actions workflow is now automatically generated. But this requires a new COMMIT.

Move to the “Git Changes” pane, enter the Commit message as shown below, click the “Commit All” button, and push the Changes.

When you actually access your GitHub repository, your GitHub action workflow runs buil and deployment.

Once deployment is complete, open a new Web browser, access the Azure Functions application URL, and find that the Swagger UI page is rendered correctly.

So far, we’ve learned how to create an OpenAPI-enabled Azure Functions application in. Net 5 runs in a standalone work environment and is deployed to Azure without leaving Visual Studio. I guess theoretically it could be. Works well on Net 6. If you’re curious, please deploy it and go to github.com/Azure/azure… Let us know!

The resources

  • Github Repository: Azure Functions OpenAPI extensions

  • Create a serverless API with Azure Function and API Management Integration in Visual Studio (Preview version)

  • Microsoft Learning Platform: Azure Functions: Discover OpenAPI and Power applications