头图

It has been a year since the preview version of Azure Functions OpenAPI Extension was released. Today, we are happy to announce that it has been officially released! While this version supports .NET Core 2.1 (LTS), 3.1 (LTS), .NET 5 and .NET 6 (LTS), it also supports in-process and out-of-process worker threads in the Azure Functions runtime.

Next, let us see how uses the OpenAPI extension in .NET 6 to create a function application in Visual Studio 2022.

In-Process Worker

In Visual Studio 2022, you can use In-Process Worker to create function applications in .NET:

Select .NET 6 → select "Http trigger with OpenAPI" in the left menu → select "Function" in Authorization level to start creating.

e9589b7771d331a4715c40c6ef682f31.png

You can see that there are several decorators starting OpenApi Since the updated Visual Studio project template for this extension has not yet been released, the OpenApi extension needs to be updated now. Select the "Dependencies" menu in the Solution Explorer, and then click the "Manage NuGet Packages..." menu. Update the existing Microsoft.Azure.WebJobs.Extensions.OpenApi package to 1.0.0 . After the update, rebuild the project and run the function application by clicking the "Debug" button at the top.

Out-of-Process Worker

In Visual Studio 2022, you can also use Out-of-Process Worker to create function applications in .NET. Since the updated Visual Studio project template for this extension is not yet available, you now need to use the existing HTTP trigger and add the NuGet package. The creation method is as follows:

935c1de7f5b550e1b8c3d4ac2ed6dbdd.png

Add NuGet Package Microsoft.Azure.Functions.Worker.Extensions.OpenApi . After installation, update Program.cs to activate the extension. Delete ConfigureFunctionsWorkerDefaults() and add ConfigureFunctionsWorkerDefaults(worker =>worker.UseNewtonsoftJson()) and ConfigureOpenApi() . Then, add the OpenAPI-related decorator to the end of the function application. After completing the update, rebuild the project and run the function application by clicking the "Debug" button at the top.

from .NET Core 3.1 or .NET 5 to .NET 6

Only need to update .csproj file, package version and target framework. Then update the NuGet packages related to Azure Functions to the latest version, including the OpenAPI extension. Rebuild the project and click the "Debug" button at the top to run the function application. Next, open the web browser and enter http://localhost:7071/api/swagger/ui, you can see the Swagger UI page.

ba82d6041092cf5ca4ff52c846804f7.png

For more detailed operation steps, please scan the QR code to view

f99ace62aae5b080405cab695c0bccf0.png


微软技术栈
423 声望996 粉丝

微软技术生态官方平台。予力众生,成就不凡!微软致力于用技术改变世界,助力企业实现数字化转型。