Swagger UI для net Core 3.1 API очень медленно - PullRequest
1 голос
/ 21 января 2020

Я обновил наше net основное 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 отображается относительно быстро. Однако при нажатии на метод в контроллере для отображения кнопки «Попробуйте» требуется 30 секунд. Есть ли способ отладить проблему? Или есть у кого такая же проблема? До преобразования кода из SwashBuckle 2.5 и net core 2.1 в SwashBuckle 5.0 и net core 3.1 пользовательский интерфейс Swagger работал очень быстро.

1 Ответ

2 голосов
/ 12 марта 2020

Вы используете 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

...