如何为独立 Framework4.8 Azure Function (v4) 生成 OpenAPI 文档

问题描述 投票:0回答:1

我没能在我的 Azure Function 中生成 OpenAPI。

它调用 SOAP Web 服务,因此必须在 Framework 4.8 中运行,因为 .NET 的非 Framework 版本不会返回整个嵌套的 XML 负载。我尝试过 Functions v1 和 v4 (Isolated),但无法生成 OpenAPI。

我错过了什么?

以下属性块存在一些错误。主要的是

'OpenApiOperation' is not an attribute class

Function.cs 的开始:

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using TGAWebSvcs;
using Newtonsoft.Json;
using System.Web.Mvc;
using System.Net.Http;
using System.Net;
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi;
using Microsoft.OpenApi.Models;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using System.Linq;
using TGAWebSvcs.TCSvc;
using Microsoft.Azure.Functions.Worker.Http;

namespace TGAFunction
{
    public static class GetData
    {

        [Function("GetServerTime")]
        [OpenApiOperation(operationId: "GetServerTime", tags: new[] { "TGA API" }, Summary = "Retrieve server time", Description = "This API returns the current server time")]
        [OpenApiParameter(name: "username", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "The **username** parameter")]
        [OpenApiParameter(name: "password", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "The **password** parameter")]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(string), Description = "The operation completed successfully")]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(string), Description = "The operation was not completed successfully")]
        public static async Task<IActionResult> GetServerTime([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req) //, ILogger logger)
        {
...

程序.cs:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Extensions;
using Microsoft.Extensions.Hosting;

internal class Program
{
    static void Main(string[] args)
    {
        FunctionsDebugger.Enable();

        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
            .ConfigureOpenApi()
            .Build();

        host.Run();
    }
}
azure-functions openapi .net-4.8
1个回答
0
投票

这适用于 .net8 独立 Azure 函数应用程序。
确保您的项目有这些参考资料:

<ItemGroup>
  <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />
<ItemGroup/>

以及这些 using 语句

using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;

这有效并为我生成了一个招摇的页面:

[Function(nameof(CreateUser))]
[OpenApiOperation(operationId: nameof(CreateUser), tags: new[] { "Create User" }, Summary = "Create User", Description = "Create User")]
[OpenApiRequestBody(contentType: ContentTypes.ApplicationJson, bodyType: typeof(UserRequest), Required = true, Description = "UserRequest")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Created, contentType: ContentTypes.ApplicationJson, bodyType: typeof(UserResponse), Summary = "User Response", Description = "UserRequest Response")]
public async Task<HttpResponseData> CreateUser(
    [HttpTrigger(AuthorizationLevel.Anonymous, HttpMethodNames.Post, Route = UserRoutes.UserRoute)]
    HttpRequestData req,
    CancellationToken cancellationToken)
{
...

不知道为什么,但不是

using Microsoft.Azure.Functions.Worker.Extensions.OpenApi;
,它对我有用
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;

希望这对您有用!

© www.soinside.com 2019 - 2024. All rights reserved.