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:&quot;$(AssemblyName)&quot; /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

Most upvoted comments

image looks like it works only with minimal api, but ignores controllers. It still works fine with old startup style.

Same Issue here, this is what I have done as a workaround for now too.

Exactly same problem, v13.14.4. The Controllers are not picked up, even though OpenApiDocumentMiddleware generates 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

<Target Name="NSwag" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
	<Message Importance="High" Text="$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)" />

	<Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development" Command="$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)" />

	<Delete Files="$(ProjectDir)\obj\$(MSBuildProjectFile).NSwag.targets" />
	<!-- This thingy trigger project rebuild -->
</Target>	

@RicoSuter Here is a draft Pull Request with example: https://github.com/RicoSuter/NSwag/pull/3734

Added the configuration under nswag.json as we are using it right now. It was working with Net5 and on Net6 when using the IHostBuilder, not the WebApplicationBuilder - it still works.

image looks like it works only with minimal api, but ignores controllers.

@RicoSuter You might need to implement a strategy using the new HostFactoryResolver API as was done in Swashbuckle in https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/67344fe0b7c7e78128159d8bf02ebfe91408c3da/src/Swashbuckle.AspNetCore.Cli/Program.cs.

The HostFactoryResolver is the mechanism by which you can resolve the HostBuilder from a target top-level program.