gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:11:02 31 4
gpt4 key购买 nike

使用 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com