简体   繁体   English

用于 net core 3.1 api 的 Swagger UI 非常慢

[英]Swagger UI for net core 3.1 api is very slow

I updated Our net core API application from 2.1 to 3.1, SwashBuckle.Asp.NetCore to 5.0.0.我将我们的网络核心 API 应用程序从 2.1 更新到 3.1,将 SwashBuckle.Asp.NetCore 更新到 5.0.0。 Here is my startup set:这是我的启动集:

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();
            });
        });
    }
}

The initial Swagger UI displays relatively quickly.初始 Swagger UI 显示相对较快。 However, when a method in a controller is clicked, it takes 30 seconds to display "Try it out" button.但是,当单击控制器中的方法时,需要 30 秒才能显示“试用”按钮。 Is there a way to debug the problem?有没有办法调试问题? Or Is there anyone having the same problem?或者有没有人遇到同样的问题? Before the code was converted from SwashBuckle 2.5 and net core 2.1 to SwashBuckle 5.0 and net core 3.1, the swagger UI works very fast.在代码从 SwashBuckle 2.5 和 net core 2.1 转换为 SwashBuckle 5.0 和 net core 3.1 之前,swagger UI 工作得非常快。

Are you using NewtonSoft?您在使用 NewtonSoft 吗? You need to add:您需要添加:

Install-Package Swashbuckle.AspNetCore.Newtonsoft -Version 5.1.0

And add:并添加:

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

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

I'm using "Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" And with that version switching to "Swashbuckle.AspNetCore.Newtonsoft" was not really helping.我正在使用“Swashbuckle.AspNetCore.SwaggerUI”版本=“5.6.3”并且该版本切换到“Swashbuckle.AspNetCore.Newtonsoft”并没有真正帮助。 There was no significant improvement.没有显着改善。

Then I have fount this issue listed on Github .然后我在 Github 上找到了这个问题 It is an old resolved issue but reopened in 2020. Where they explain Swagger UI 3.x has "Pretty print" and "Syntax highlight" which causing the render issues .这是一个已解决的旧问题,但在 2020 年重新开放。他们解释 Swagger UI 3.x 的地方有“漂亮的打印”和“语法高亮”,这会导致渲染问题 It can be turned off in Swagger config:它可以在 Swagger 配置中关闭:

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

In .NET Core you can access config as well: Setup.cs in Configure()在 .NET Core 中,您也可以访问配置: Configure() Setup.cs

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...
    });

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

I faced a similar problem with swagger in my .Net 5 Web API project and it was fixed after following the steps and adding the code mentioned in both the above answers.我在我的 .Net 5 Web API 项目中遇到了类似的 swagger 问题,在按照步骤并添加上述两个答案中提到的代码后,它得到了修复。 To summarize:总结一下:

  1. Installed package Swashbuckle.AspNetCore.Newtonsoft 6.1.4安装包 Swashbuckle.AspNetCore.Newtonsoft 6.1.4
  2. This line was already there in the Startup.cs: services.AddSwaggerGenNewtonsoftSupport();这行已经在 Startup.cs 中: services.AddSwaggerGenNewtonsoftSupport();
  3. Added 2 lines of code in the Configure() of Startup.cs: (c.ConfigObject.AdditionalItems...)在Startup.cs的Configure()中添加了两行代码:(c.ConfigObject.AdditionalItems...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM