NSwag: aspnet 6 and v13.14.0: The Startup class could not be determined in the assembly
Trying the new v13.14.0 build and I am getting errors when targeting .NET 6 with the following build step in the webapi project:
<Target Name="NSwag" AfterTargets="Build">
<Exec Command="$(NSwagExe_Net60) aspnetcore2swagger /infotitle:"$(AssemblyName)" /assembly:$(TargetPath) /output:$(OutDir)$(AssemblyName).swagger.json" />
</Target>
1>System.InvalidOperationException: The Startup class could not be determined in the assembly 'Template.User.Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
1> at NSwag.Commands.Generation.OpenApiGeneratorCommandBase`1.CreateWebHostAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\OpenApiGeneratorCommandBase.cs:line 345
1> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 323
1> at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader`1.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 76
1> at NSwag.Commands.IsolatedCommandBase`1.<>c__DisplayClass17_0.<RunIsolatedAsync>b__0() in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
1> at System.Threading.Tasks.Task`1.InnerInvoke()
1> at System.Threading.Tasks.Task.<>c.<.cctor>b__271_0(Object obj)
1> at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
1>--- End of stack trace from previous location ---
1> at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
1> at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
1>--- End of stack trace from previous location ---
1> at NSwag.Commands.IsolatedCommandBase`1.RunIsolatedAsync(String configurationFile) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
1> at NSwag.Commands.IsolatedSwaggerOutputCommandBase`1.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedSwaggerOutputCommandBase.cs:line 51
1> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 98
1> at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
1> at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
1> at NConsole.CommandLineProcessor.Process(String[] args, Object input)
1> at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 55
The startup code template has been changed aspnet 6 webapi code template, so that might be a contributing factor.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 47 (19 by maintainers)
Commits related to this issue
- Add controller to minimal API, #3711 — committed to RicoSuter/NSwag by RicoSuter 3 years ago
- Add controller to minimal API, #3711 — committed to lahma/NSwag by RicoSuter 3 years ago
Exactly same problem, v13.14.4. The Controllers are not picked up, even though
OpenApiDocumentMiddlewaregenerates the swagger properly.As result the clients are empty.
@RicoSuter as it’s seems to be a blocker to a lot of people to move into minimal API with controllers, is there something out to target it?
I’ve run into this issue too (see #3727) - see this repo; https://github.com/jasontaylordev/BlazorWasmHosted. If you try to generate a specification using NSwagStudio and the config.nswag it will work, it just won’t find any operations.
If I adjust this code to use the old Startup / Program classes, then it will generate the specification without issue.
In either case, using app.UseOpenApi() middleware, will generate the specification at runtime without issue.
switching to project does not work since the structure of program.cs has changed for .net 6 and Im getting the following error
1> —> System.InvalidOperationException: NSwag requires the entry point type Program to have either an BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method. See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x for suggestions on ways to refactor your startup type.
Im using the NSwag Build lib
@RicoSuter Here is a draft Pull Request with example: https://github.com/RicoSuter/NSwag/pull/3734
Added the configuration under
nswag.jsonas we are using it right now. It was working with Net5 and on Net6 when using theIHostBuilder, not theWebApplicationBuilder- it still works.@RicoSuter You might need to implement a strategy using the new
HostFactoryResolverAPI as was done in Swashbuckle in https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/67344fe0b7c7e78128159d8bf02ebfe91408c3da/src/Swashbuckle.AspNetCore.Cli/Program.cs.The
HostFactoryResolveris the mechanism by which you can resolve theHostBuilderfrom a target top-level program.correct me if im wrong, but this looks very similar to EF Migration which has same issue with .net 6 WebApplicationBuilder
https://github.com/dotnet/efcore/blob/fa3cea4fc4cfd65dd87995f4d72bb46dd550db22/src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs#L49
Here is the “work around” https://andrewlock.net/exploring-dotnet-6-part-5-supporting-ef-core-tools-with-webapplicationbuilder/