RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.7k stars 1.24k forks source link

NSwag.MSBuild: Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0 #1394

Open marobet opened 6 years ago

marobet commented 6 years ago

Hello, I'm working on ASP.NET Core Web App, targeting framework netcoreapp2.1 I'm referencing package NSwag.MSBuild Version="11.17.15" And this is the line I have in my .csproj file

  <Target Name="NSwag" AfterTargets="Build">
    <Exec Command="$(NSwagExe) webapi2swagger /assembly:$(OutDir)$(AssemblyName).dll  /output:$(OutDir)swagger.json" />
  </Target>

when building I'm getting below error message,

NSwag command line tool for .NET 4.6.1+ WinX64, toolchain v11.17.15.0 (NJsonSchema v9.10.53.0 (Newtonsoft.Json v9.0.0.0)) Visit http://NSwag.org for more information. NSwag bin directory: C:\Users\.nuget\packages\nswag.msbuild\11.17.15\build\Win System.IO.FileNotFoundException: Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. File name: 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Server stack trace: at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes) at System.Reflection.RuntimeAssembly.GetExportedTypes() at NSwag.SwaggerGeneration.WebApi.WebApiToSwaggerGenerator.GetControllerClasses(Assembly assembly) at System.Linq.Enumerable.d172.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.OrderedEnumerable`1.d1.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at NSwag.Commands.SwaggerGeneration.WebApi.WebApiToSwaggerCommand.<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.IsolatedCommandBase1.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.IsolatedCommandBase1.IsolatedCommandAssemblyLoader1.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) at NSwag.Commands.IsolatedCommandBase1.<>c__DisplayClass13_0.<RunIsolatedAsync>b__0() at System.Threading.Tasks.Task1.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.d13.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.d9.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.d12.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.d11.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)

when checked https://www.nuget.org/packages/System.ComponentModel.Annotations Version=4.2.0.0, is no longer available in nuget.

RicoSuter commented 6 years ago

Try copying the required dlls to the output dir first: https://github.com/RSuter/NSwag/issues/1159#issuecomment-363886104

RicoSuter commented 6 years ago

Besides that i recommend using the new api explorer generator aspnetcore2swagger (AspNetCoreToSwaggerGenerator)

marobet commented 6 years ago

tried the approach #1159 (comment) didn't work :( trying now using AspNetCoreToSwaggerGenerator.

RicoSuter commented 6 years ago

Hmm youll probably see the same error as it works more or less the same. You could use the csproj option there (instead of assembly) but you probably have a recursion bevause it will rebuild the project (maybe use nobuild option and csproj).

Otherwise please provide a reproduction sample so we can look into it...

marobet commented 6 years ago

The error comes up when using a class as a parameter for the controller method, or use DataAnnotations to perform model validation.

Here is a simple project where the error message can be reproduced. https://github.com/marobet/nswag.example/

Thank you