asp.net-core - .NET Core 3.1 - Swagger 未加载

标签 asp.net-core swagger .net-core-3.1

我正在尝试在我的 WebApi 中实现 Swagger。但是,它不起作用。问题是 swagger.json 永远加载。当我尝试去/swagger/v1/swagger.json ,它永远加载,我的 CPU 使用率飙升至 100%。

这是我的配置:

public class Startup
{
    ...

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
       ...

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo
            {
                Version = "v1",
                Title = "ToDo API"
            });
        });

       ...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services)
    {
       ...

        app.UseSwagger();

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });

        // Setup the endpoints.
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action}/{id?}");
            endpoints.MapHub<HomeHub>("/homehub");
        });
    }
}

我当然已经在互联网上环顾四周,但找不到任何解决方案。我没有收到任何错误,那么可能是什么问题?

更新:

我试过用 CLI 生成 json 文件,同样的问题。它只是加载,什么也没有发生。除了占用 24GB 内存...

最佳答案

我相信您的 Controller 或类之一会导致此错误。看起来 swagger 试图递归加载一些东西。

我建议你使用内存分析器(比如 dotMemory)来找出发生了什么(是什么造成了这个内存泄漏)

另一种解决方法是注释掉所有 Controller 并一个一个启用它们,直到找到错误代码。

您也可以查看 this Answer

关于asp.net-core - .NET Core 3.1 - Swagger 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61580858/

相关文章:

asp.net - 在 ASP.Net Core 2.2 中定义不同的 API 以接受不同的大小限制

c# - 具有自定义角色授权的 ASP.Net Core 3.0 Windows 身份验证

asp.net - 没有 Duende 身份服务器的 Blazor

c# - 什么是 PowerShell.Telemetry.ApplicationInsightsTelemetry?我可以将其关闭吗?

azure - 在 Durable Functions 中等待外部事件

c# - JsonSerializer.Deserialize 失败

c# - 使用 OData 查询字符串时抛出 MissingManifestResourceException

maven - 适用于 OpenAPI 3.0 的 Swagger Codegen(带有 Maven 插件)

spring-boot - 如何更改<主机 :port> to domain in swagger

swagger - Swagger UI 上的 CORS 问题