Swashbuckle Swagger 生成器:StringBuilder 内存不足

Swashbuckle Swagger Generator: StringBuilder out of Memory

我们有一个相当大的 ASP.NET Boilerplate .NET Core 3.1 项目,Swashbuckle 在生成 Swagger JSON 时似乎 [=2=] 内存不足 JSON。

我们遇到以下错误:

RequestAborted: Exception of type 'System.OutOfMemoryException' was thrown.. Request Path: /swagger/v1/swagger.json
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Text.StringBuilder.ToString()
   at System.IO.StringWriter.ToString()
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
   at Abp.AspNetZeroCore.Web.Authentication.JwtBearer.JwtTokenMiddleware.<>c__DisplayClass0_0.<<UseJwtTokenMiddleware>b__0>d.MoveNext()

自然这表明 StringBuilder 无法处理 JSON 字符串的大小。我不知道 StringBuilder 有任何特定的内存限制,所以不确定为什么会失败。

我们追踪到了这个问题,它与我们的控制器/应用程序服务上的以下方法有关。

public MyEnum GetMyEnum(MyDto myDto, long? someValue = null)

看来是默认参数值引起的。我们能够通过将方法标记为 private.

来移除这种依赖性