diff --git a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs index a39b1711..5bcfd1d7 100644 --- a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs +++ b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/GlobalUsings.cs @@ -1,6 +1,7 @@ // Global using directives global using System; +global using System.Collections.Concurrent; global using System.Collections.Generic; global using System.Linq; global using System.Net; @@ -29,6 +30,7 @@ global using Microsoft.OpenApi.Models; global using Serilog; global using Serilog.Exceptions; global using Serilog.Sinks.Elasticsearch; +global using Swashbuckle.AspNetCore.Swagger; global using Swashbuckle.AspNetCore.SwaggerGen; global using Volo.Abp; global using Volo.Abp.AspNetCore.ExceptionHandling; diff --git a/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/CachingSwaggerProvider.cs b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/CachingSwaggerProvider.cs new file mode 100644 index 00000000..cfd40fd5 --- /dev/null +++ b/aspnet-core/shared/Lion.AbpPro.Shared.Hosting.Microservices/Swagger/CachingSwaggerProvider.cs @@ -0,0 +1,26 @@ +namespace Swagger; + +/// +/// swagger注释加载慢,把文档添加到缓存 +/// +[Dependency(ReplaceServices = true)] +[ExposeServices(typeof(ISwaggerProvider))] +public class CachingSwaggerProvider : ISwaggerProvider, ITransientDependency +{ + private static readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + private readonly SwaggerGenerator _swaggerGenerator; + + public CachingSwaggerProvider( + IOptions optionsAccessor, + IApiDescriptionGroupCollectionProvider apiDescriptionsProvider, + ISchemaGenerator schemaGenerator) + { + _swaggerGenerator = new SwaggerGenerator(optionsAccessor.Value, apiDescriptionsProvider, schemaGenerator); + } + + public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null) + { + return _cache.GetOrAdd(documentName, (_) => _swaggerGenerator.GetSwagger(documentName, host, basePath)); + } +} \ No newline at end of file