当响应太大约 2mb 时,Swagger UI 会冻结

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

我已使用 Microsoft.Azure.Functions.Worker.Extensions.OpenApi 将 OpenAPI 文档添加到我的 Function 应用程序项目中,但是当响应太大(大约 2mb)时,swagger/ui 会冻结。我能做点什么吗?

我的函数返回大响应:

[Function("GetBookableResources")]
[OpenApiOperation(operationId: "GetBookableResources", tags: new[] { "Bookable Resource" }, Summary = "Get Bookable Resources", Description = "", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiRequestBody("application/json", typeof(BookableResourcesRequestBody),Description = "JSON request body", Required = true)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(List<BookableResourcesResponse>), Summary = "The response", Description = "This returns the response")]
public IActionResult GetBookableResources([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req, [FromBody] BookableResourcesRequestBody reqBody)
{
    try
    {   
        var bookableResources = _bookableResourceService.GetBookableResources(reqBody);

        return new OkObjectResult(bookableResources);
    }
    catch (Exception ex)
    {
        _logger.LogError($"Get Bookable resources error: {ex.Message}");
        return new BadRequestObjectResult(ex.Message);
    }
}

程序.cs:

services.AddSingleton<IOpenApiConfigurationOptions>(_ =>
{
    var options = new OpenApiConfigurationOptions()
    {
        Info = new OpenApiInfo()
        {
            Version = "1.0.0",
            Title = "API",
            Description = "This is API."
        },
        Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
        OpenApiVersion = OpenApiVersionType.V3,
        IncludeRequestingHostName = true,
        ForceHttps = false,
        ForceHttp = false
    };

    return options;
});
c# openapi swagger-ui azure-functions-isolated
1个回答
0
投票

这是 Swagger UI 和大量响应的“已知问题”。 您使用的库是否提供配置来在 Swagger UI 中

禁用语法突出显示

?如果是,可能有助于提高性能。 还可以考虑为“获取项目列表”端点实现

分页

,并将数据检索限制为一次最多 N 个项目(例如 100 个)。这将有助于提高您的服务器性能和 API 消费者的开发人员体验。

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