Swagger Generator (msbuild) fails on full .NET Framework with Microsoft.AspNetCore.Mvc 2.2
See original GitHub issueThe failing combination is exactly net462 + Microsoft.AspNetCore.Mvc 2.2.0. Works fine with netcoreapp2.2 + Microsoft.AspNetCore.Mvc 2.2.0 and net462 + Microsoft.AspNetCore.Mvc 2.1.3.
Installed packages:
- Microsoft.AspNetCore.Mvc 2.2.0
- NSwag.AspNetCore 12.1.0
- NSwag.MSBuild 12.1.0
Invoking after build target
$(NSwagExe) aspnetcore2swagger "/assembly:$(TargetPath)" "/output:$(OutputPath)swagger.json"
The command fails with the following exception:
NSwag command line tool for .NET 4.6.1+ WinX64, toolchain v12.1.0.0 (NJsonSchema v9.13.28.0 (Newtonsoft.Json v11.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: C:\Users\appveyor\.nuget\packages\nswag.msbuild\12.1.0\tools\Win
System.InvalidOperationException: The ConfigureServices method must either be parameterless or take only one parameter of type IServiceCollection.
Server stack trace:
at Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at NSwag.Commands.SwaggerGeneration.SwaggerGeneratorCommandBase`1.<CreateWebHostAsync>d__110.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.SwaggerGeneration.AspNetCore.AspNetCoreToSwaggerCommand.<RunIsolatedAsync>d__44.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader`1.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths)
at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader`1.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths)
at NSwag.Commands.IsolatedCommandBase`1.<>c__DisplayClass17_0.<RunIsolatedAsync>b__0()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.IsolatedCommandBase`1.<RunIsolatedAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.IsolatedSwaggerOutputCommandBase`1.<RunAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.SwaggerGeneration.AspNetCore.AspNetCoreToSwaggerCommand.<RunAsync>d__39.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.<ProcessSingleAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.<ProcessAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.Process(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.Process(String[] args)
C:\projects\nswag-issue-mvc22\net462-mvc21\nswag.targets(7,9): error MSB3073: The command ""C:\Users\appveyor\.nuget\packages\nswag.msbuild\12.1.0\build\../tools/Win/NSwag.exe" aspnetcore2swagger "/assembly:C:\projects\nswag-issue-mvc22\net462-mvc22\bin\Debug\net462\net462-mvc22.dll" "/output:bin\Debug\net462\swagger.json"" exited with code -1. [C:\projects\nswag-issue-mvc22\net462-mvc22\net462-mvc22.csproj]
I assume that this is related to assembly loading once again, probably the assembly that contains the IServiceCollection interface got loaded into a different load context or something like that. I did try to dig around NSwag sources but unfortunately failed to find a fix.
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (8 by maintainers)
Top Results From Across the Web
Get started with NSwag and ASP.NET Core
NSwag offers the following capabilities: The ability to utilize the Swagger UI and Swagger generator. Flexible code generation capabilities.
Read more >Troubleshoot .NET Framework targeting errors
This article provides resolutions for MSBuild errors that might occur because of reference issues.
Read more >Automatically generating C# API clients on build with NSwag
Configure Swagger and SwaggerUI with NSwag. First add the NSwag.AspNetCore NuGet package to your API project: <Project Sdk="Microsoft.NET.
Read more >Add NuGet package XML documentation to Swagger
At my current client, we are building an API that is put together by re-usable “API parts”, eg. ASP.NET Core Application Parts, which...
Read more >ASP.NET Core 2.2 (framework) project fails to build
Create a new ASP.NET Core Web project, target .NET Framework and choose the MVC template. Upgrade all NuGet packages to the latest versions....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Created a PR to fix this: https://github.com/RicoSuter/NSwag/pull/2096
I’ve been getting this exception with .Net 4.7.2 and MVC 2.1.3, though I haven’t tried with such a small repro yet so it’s possible it could be caused by some other factor.