c# - Net core 3.1 api 的 Swagger UI 非常慢
问题描述
我将我们的 net core 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 秒才能显示“试用”按钮。有没有办法调试问题?或者有没有人有同样的问题?在代码从 SwashBuckle 2.5 和 net core 2.1 转换为 SwashBuckle 5.0 和 net core 3.1 之前,swagger 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
推荐阅读
- sql - 嵌套 (?) SQL 查询
- ios - URLSession downloadTask(with:) 创建多个文件下载
- javascript - Javascript InnerHtml 来自
- reactjs - 无法将反应应用程序部署到谷歌应用程序引擎
- angular - 只为包输出和运行测试构建一次 Angular 9 项目
- javascript - 在 Angular 中打开新项目时折叠已经消耗的项目
- google-calendar-api - Google 日历 API 将会议数据返回为待处理
- reactjs - 找不到模块:无法解析“react-table/react-table.css”
- google-drive-api - Google Colab 磁盘空间与 Google Drive 磁盘空间
- maven-2 - 父 Pom 获取子 Pom/依赖的版本