[英]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 秒才能顯示“試用”按鈕。 有沒有辦法調試問題? 或者有沒有人遇到同樣的問題? 在代碼從 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
我正在使用“Swashbuckle.AspNetCore.SwaggerUI”版本=“5.6.3”並且該版本切換到“Swashbuckle.AspNetCore.Newtonsoft”並沒有真正幫助。 沒有顯着改善。
然后我在 Github 上找到了這個問題。 這是一個已解決的舊問題,但在 2020 年重新開放。他們解釋 Swagger UI 3.x 的地方有“漂亮的打印”和“語法高亮”,這會導致渲染問題。 它可以在 Swagger 配置中關閉:
SwaggerUI({
syntaxHighlight: {
activated: false,
theme: "agate"
},
//url: path,
....
});
在 .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>
我在我的 .Net 5 Web API 項目中遇到了類似的 swagger 問題,在按照步驟並添加上述兩個答案中提到的代碼后,它得到了修復。 總結一下:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.