ABP/Swashbuckle - 使用 Swashbuckle CLI 生成 swagger 文档

标签 abp swashbuckle.aspnetcore

使用 ABP 框架 (3.3) 和 ASP.NET Core (3.1)。

在编译应用程序后,我尝试使用 Swashbuckle.AspNetCore.Cli ( https://github.com/domaindrivendev/Swashbuckle.AspNetCore#retrieve-swagger-directly-from-a-startup-assembly ) 生成 swagger.json,使用以下命令指向已编译的 dll:

dotnet swagger tofile --output "C:\temp\swagger".\bin\Release\netcoreapp3.1\MyBin.dll v1

它给了我以下异常:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'provider')
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionCommonExtensions.GetRequiredService[T](IServiceCollection services)
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Volo.Abp.AspNetCore.Mvc.AbpDataAnnotationAutoLocalizationMetadataDetailsProvider.CreateDisplayMetadata(DisplayMetadataProviderContext context)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultCompositeMetadataDetailsProvider.CreateDisplayMetadata(DisplayMetadataProviderContext context)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_DisplayMetadata()
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_Order()
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.<>c.<get_Properties>b__78_0(ModelMetadata p)
   at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
   at System.Linq.EnumerableSorter`1.ComputeMap(TElement[] elements, Int32 count)
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ModelPropertyCollection..ctor(IEnumerable`1 properties)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_Properties()
   at Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadataProviderExtensions.GetMetadataForProperty(IModelMetadataProvider provider, Type containerType, String propertyName)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.DefaultApplicationModelProvider.CreatePropertyModel(PropertyInfo propertyInfo)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.DefaultApplicationModelProvider.OnProvidersExecuting(ApplicationModelProviderContext context)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory.CreateApplicationModel(IEnumerable`1 controllerTypes)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors()
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection()
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize()
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.get_ActionDescriptors()
   at Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionGroupCollectionProvider.get_ApiDescriptionGroups()
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath)
   at Swashbuckle.AspNetCore.Cli.Program.<>c.<Main>b__0_3(IDictionary`2 namedArgs) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\Program.cs:line 72
   at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\CommandRunner.cs:line 68
   at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\CommandRunner.cs:line 68
   at Swashbuckle.AspNetCore.Cli.Program.Main(String[] args) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\Program.cs:line 111

我还尝试根据 Swashbuckle 文档创建一个公共(public)类,因为我们使用的是 Autofac:

    public class SwaggerHostFactory
    {
        public static IHost CreateHost()
        {
            return Program.CreateHostBuilder(new string[0]).Build(); //Calls the same builder that's used to run the app
        }
    }

目的是允许在我们的管道中运行它以自动生成 ApiClient 库。

对于如何解决这个问题,我有点不知所措。 swagger.json 是在运行时生成的吗?

感谢任何帮助!

最佳答案

避免这种情况的一种方法是确保您的程序在没有这些应用程序设置的情况下继续运行

var connectionString = config["MyConnectionString"];
if (connectionString != null)
{
    services.AddDbContext<AppDbContext>(options =>
        options.UseSqlServer(connectionString));
}

关于ABP/Swashbuckle - 使用 Swashbuckle CLI 生成 swagger 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65623958/

相关文章:

.net-core - 将 dotnetcore 5 web api 部署到 iis 给出 404

c# - 使用 Swagger 在 ASP.NET Core 中实现 OAuth

c# - 在 Azure 中的 ASP.NET Core 3.1 应用程序中获取 swagger-ui 返回 404

abp - 关于使用 DomainService

abp - “abp”不被识别为内部或外部命令,

ubuntu - 如何将 ABP 框架应用程序部署到 Ubuntu 服务器?

asp.net-core - 刷新页面时连接握手失败和 500 错误 - Angular 和 .Net Core 的 Abp 零

.net - 解决混合内容错误的Swagger设置

AspNetBoilerplate 不从数据库加载设置

c# - 是否可以在 Swagger UI 的参数部分显示请求主体对象?