.NET 6.0+Azure Functions v4下Swagger CLI生成OpenAPI文件遇重载错误
编辑 - 已定位问题并在Github提交了Issue
经过多日调研,未找到可靠解决方案,但发现有博客实现了类似Azure Functions配置,因此认为该需求可实现。
问题
当前尝试通过Swagger CLI和MSBuild生成openapiv3.json文件,作为CI/CD流程的构建产物导入Azure API管理(APIM),而非部署后依赖运行中的函数获取。现有配置可生成swagger.json和openapi/v3.json端点并成功导入APIM,但生成构建产物时始终报错:
C:\MyApiFolder [t/238123_OpenApiGen ≡ +1 ~2 -0 !]> dotnet tool run swagger tofile --output openapi.json MyApi/bin/Debug/net6.0/MyApi.dll v1 Unhandled exception. System.InvalidOperationException: Having multiple overloads of method 'Configure' is not supported. at Microsoft.AspNetCore.Hosting.StartupLoader.FindMethod(Type startupType, String methodName, String environmentName, Type returnType, Boolean required) at Microsoft.AspNetCore.Hosting.StartupLoader.FindConfigureDelegate(Type startupType, String environmentName) at Microsoft.AspNetCore.Hosting.StartupLoader.LoadMethods(IServiceProvider hostingServiceProvider, Type startupType, String environmentName, Object instance) at Microsoft.AspNetCore.Hosting.WebHostBuilder.<>c__DisplayClass14_1.<BuildCommonServices>b__4(IServiceProvider sp) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.AspNetCore.Hosting.WebHost.EnsureStartup() at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.WebHost.Initialize() at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at Swashbuckle.AspNetCore.Cli.Program.GetServiceProvider(Assembly startupAssembly) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\Program.cs:line 145 at Swashbuckle.AspNetCore.Cli.Program.<>c.<Main>b__0_4(IDictionary`2 namedArgs) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\Program.cs:line 82 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 59 at Swashbuckle.AspNetCore.Cli.Program.Main(String[] args) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\Program.cs:line 121
环境配置
使用的环境如下:
- .NET 6.0
- Azure Functions v4
Microsoft.Azure.WebJobs.Extensions.OpenApi版本1.5.1(用于生成OpenApi文档)Swashbuckle.AspNetCore版本6.5.0
上述OpenApi扩展为Azure官方提供的Functions OpenApi扩展。尝试使用Swashbuckle.AspNetCore NuGet包中的Swagger CLI(版本6.5.0,由AzureExtensions.Swashbuckle版本3.3.2提供),并已按照Swashbuckle官方文档添加Swashbuckle到项目中。
示例函数代码:
[FunctionName("CreateClient")] [OpenApiOperation(operationId: "CreateClient", tags: new[] { "client", "create" })] [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)] [OpenApiRequestBody("application/json", typeof(CreateClient))] [OpenApiResponseWithBody(statusCode: HttpStatusCode.Created, contentType: "application/json", bodyType: typeof(string), Description = "Success Response")] [OpenApiResponseWithBody(statusCode: HttpStatusCode.Conflict, contentType: "application/json", bodyType: typeof(string), Description = "Returned when item exists")] [OpenApiResponseWithBody(statusCode: HttpStatusCode.InternalServerError, contentType: "application/json", bodyType: typeof(string), Description = "Returned for unhandled errors")]
Startup.cs中Swashbuckle/Swagger配置(尝试过多种组合,仅AddSwaggerGen始终保留):
public override void Configure(IFunctionsHostBuilder builder) { // 其他服务注册 builder.Services.AddSingleton<IOpenApiConfigurationOptions, OpenApiConfiguration>() .AddSwashBuckle(Assembly.GetExecutingAssembly()) .AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiConfiguration().Info)) .AddSwaggerGenNewtonsoftSupport(); }
同时在csproj中添加配置以输出runtimeconfig.json:
<PropertyGroup> <TargetFramework>net6.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> </PropertyGroup>
补充说明
使用了一些外部NuGet包添加函数绑定,这些扩展包含使用IWebJobsBuilder的Configure方法,尝试移除后问题仍存在。示例:
public void Configure(IWebJobsBuilder builder) => builder.AddExtension<StartupTriggerExtensionConfigProvider>();
已尝试方案
- 更换
AzureExtensions.Swashbuckle版本:Swashbuckle.AspNetCore6.5.0面向.NET7.0,通过global.json设置RollForward解决版本兼容,但仍报错。
global.json配置:{ "sdk": { "rollForward": "latestMinor", "version": "6.0.114" } } - 使用注入IFunctionsHostBuilder实现SwashbuckleStartup:无效且已标记为[Obsolete],尽量避免。
- 按照Swashbuckle官方文档配置自定义Host:未解决问题。
寻求可行的解决方案或规避方法。
内容的提问来源于stack exchange,提问作者Matthew Alltop
相关产品推荐
相关产品推荐

