c# - Net Core 3.1 API的Swagger UI非常慢

标签 c# .net-core swagger swagger-ui swashbuckle

我将我们的网络核心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用户界面显示相对较快。但是,单击 Controller 中的方法时,需要30秒才能显示“试用”按钮。有没有办法调试问题?还是有人遇到同样的问题?
在将代码从SwashBuckle 2.5和网络核心2.1转换为SwashBuckle 5.0和网络核心3.1之前,一般的UI会非常快速地工作。

最佳答案

您在使用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

关于c# - Net Core 3.1 API的Swagger UI非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59849045/

相关文章:

c# - 如何在 amazon s3 bucket 中创建子目录

c# - 当我尝试将枚举格式化为字符串时,为什么我的格式会出错?

jaxb - 将 Swagger 与 JAX-RS 和 JAXB 集成需要帮助

azure - 使用 Azure 和 .Net 在事件发生后、5 天后和 15 天后发送邮件通知

c# - 反射仅在 Blazor-State-Management 中的第一次调用时成功

spring-boot - Swagger 中的 @ApiOperation 与 @ApiResponse

java - 使用 Springfox 记录 Spring 应用程序中的 jax-rs 服务

c# - Asp.net中的Email Delivery Message(如何查看邮件是否发送?)

c# - 在新的 WPF 项目中使用 Metro UI 是否值得?

linux - Linux 上的 .NET Core 是否支持 Visual Basic?