Swagger UI for net core 3.1 api 非常慢

Swagger UI for net core 3.1 api is very slow

我将我们的网络核心 API 应用程序从 2.1 更新到 3.1,SwashBuckle.Asp.NetCore 到 5.0.0。这是我的启动设置:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
     string authServerUrl = "http://testserver.com/identityserver4";
         services.AddControllersWithViews();

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

            // Define the OAuth2.0 scheme that's in use (i.e. Implicit Flow)
            c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
            {
                Type = SecuritySchemeType.OAuth2,
                Flows = new OpenApiOAuthFlows
                {
                    AuthorizationCode = new OpenApiOAuthFlow
                    {
                            AuthorizationUrl = new Uri(authServerUrl + "connect/authorize"),
                            TokenUrl = new Uri(authServerUrl + "connect/token"),
                            Scopes = new Dictionary<string, string>
                            {
                                { "netCoreAPI.read", "read permission" },
                                { "netCoreAPI.write", "write permission" }
                            }                        }
                }
            });

            c.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
                    },
                    new[] { "netCoreAPI.read", "netCoreAPI.write" }
                }
            });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("swagger/v1/swagger.json", "NetCore V1");
                c.EnableDeepLinking();
                c.OAuthClientId("clientId");
                c.OAuthClientSecret("clientSecret");
                c.OAuthAppName("netCoreApp");
                c.OAuthScopeSeparator(" ");
                c.OAuthUsePkce();
            });
        });
    }
}

初始 Swagger UI 显示相对较快。但是,当单击控制器中的方法时,需要 30 秒才能显示 "Try it out" 按钮。有没有办法调试问题?或者有没有人有同样的问题? 在代码从SwashBuckle 2.5和net core 2.1转换到SwashBuckle 5.0和net core 3.1之前,swaggerUI运行速度非常快

您在使用 NewtonSoft 吗? 您需要添加:

Install-Package Swashbuckle.AspNetCore.Newtonsoft -Version 5.1.0

并添加:

services.AddSwaggerGenNewtonsoftSupport();
// explicit opt-in - needs to be placed after AddSwaggerGen()

https://github.com/domaindrivendev/Swashbuckle.AspNetCore#systemtextjson-stj-vs-newtonsoft

我正在使用 "Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" 并且该版本切换到 "Swashbuckle.AspNetCore.Newtonsoft “ 并没有真正帮助。没有明显改善。

然后我有字体this issue listed on Github。这是一个已解决的老问题,但在 2020 年重新开放。他们解释 Swagger UI 3.x 有“Pretty print”和 “Syntax highlight”导致渲染问题 .它可以在 Swagger 配置中关闭:

SwaggerUI({
        syntaxHighlight: {
          activated: false,
          theme: "agate"
        },
        //url: path,
        ....
      });

在 .NET Core 中,您也可以访问配置:Setup.cs in Configure()

app.UseSwagger()
    .UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Web API");
        c.ConfigObject.AdditionalItems.Add("syntaxHighlight", false); //Turns off syntax highlight which causing performance issues...
        c.ConfigObject.AdditionalItems.Add("theme", "agate"); //Reverts Swagger UI 2.x  theme which is simpler not much performance benefit...
    });

我在我的 .Net 5 Web API 项目中遇到了类似的 swagger 问题,在按照上述两个答案中的步骤添加代码后,问题得到了修复。总结:

  1. 安装包Swashbuckle.AspNetCore.Newtonsoft6.1.4
  2. 此行已存在于 Startup.cs 中: services.AddSwaggerGenNewtonsoftSupport();
  3. 在 Startup.cs 的 Configure() 中添加了 2 行代码:(c.ConfigObject.AdditionalItems...)

在 csproj 中允许二进制序列化为我加快了速度:<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>