Nswag: ASP.NET Core 2.1 Preview: FileNotFoundException for System.Diagnostics.Tools

Created on 14 Mar 2018  路  19Comments  路  Source: RicoSuter/NSwag

Using NSwag.MSBuild v11.15.4

Updated my ASP.NET Core application to TargetFramework netcoreapp2.1, build now fails with the below error.

1>NSwag command line tool for .NET Core NetCore20, toolchain v11.15.4.0 (NJsonSchema v9.10.29.0 (Newtonsoft.Json v9.0.0.0))
1>Visit http://NSwag.org for more information.
1>NSwag bin directory: C:\Users\c-tjarzombek\.nuget\packages\nswag.msbuild\11.15.4\build\NetCore20
1>System.IO.FileNotFoundException: Could not load file or assembly 'System.Diagnostics.Tools, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
1>File name: 'System.Diagnostics.Tools, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: Could not load the specified file.
1>File name: 'System.Diagnostics.Tools'
1>   at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName assemblyName)
1>   at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
1>   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
1>   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
1>   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
1>   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
1>   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
1>   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
1>   at System.Attribute.GetCustomAttributes(MemberInfo element, Boolean inherit)
1>   at NSwag.SwaggerGeneration.WebApi.WebApiToSwaggerGenerator.<>c.<GetControllerClasses>b__3_4(Type t) in C:\projects\nswag\src\NSwag.SwaggerGeneration.WebApi\WebApiToSwaggerGenerator.cs:line 58
1>   at System.Linq.Utilities.<>c__DisplayClass1_0`1.<CombinePredicates>b__0(TSource x)
1>   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
1>   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
1>   at System.Linq.Enumerable.SelectEnumerableIterator`2.ToArray()
1>   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
1>   at System.Linq.OrderedEnumerable`1.ToArray()
1>   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
1>   at NSwag.Commands.SwaggerGeneration.WebApiToSwaggerCommand.GetControllerNames(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\WebApiToSwaggerCommand.cs:line 268
1>   at NSwag.Commands.SwaggerGeneration.WebApiToSwaggerCommand.<RunIsolatedAsync>d__91.MoveNext() in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\WebApiToSwaggerCommand.cs:line 192
1>--- End of stack trace from previous location where exception was thrown ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
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 60
1>   at NSwag.Commands.IsolatedCommandBase`1.<>c__DisplayClass13_0.<RunIsolatedAsync>b__0() in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 50
1>   at System.Threading.Tasks.Task`1.InnerInvoke()
1>   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
1>   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
1>--- End of stack trace from previous location where exception was thrown ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1>   at NSwag.Commands.IsolatedCommandBase`1.<RunIsolatedAsync>d__13.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1>   at NSwag.Commands.IsolatedSwaggerOutputCommandBase.<RunAsync>d__4.MoveNext() in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedSwaggerOutputCommandBase.cs:line 24
1>--- End of stack trace from previous location where exception was thrown ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1>   at NConsole.CommandLineProcessor.<ProcessSingleAsync>d__12.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
1>   at NConsole.CommandLineProcessor.<ProcessAsync>d__11.MoveNext()
1>--- End of stack trace from previous location where exception was thrown ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
1>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
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 54
1>

From my csproj:

  <Target Name="NSwag" AfterTargets="Build">
    <Copy SourceFiles="@(Reference)" DestinationFolder="$(OutDir)References" />
    <Exec Command="$(NSwagExe_Core20) webapi2swagger /assembly:$(OutDir)$(AssemblyName).dll /output:$(OutDir)swagger.json" />
    <Exec Command="$(NSwagExe_Core20) swagger2tsclient /input:$(OutDir)swagger.json /output:$(OutDir)NSwag-Generated/$(AssemblyName).ts" />
    <RemoveDir Directories="$(OutDir)References" />
  </Target>
bug enhancement

Most helpful comment

Looking forward to when NetCore21 can be added. It's now in preview 2 release (I'm having to use it for EF Core functionality).

Studio can load the DLL and find the controllers, but then I get this error:

System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.IAsyncStateMachine' from assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Try running the document in another runtime, e.g. /runtime:NetCore20

I assume with the proper target this will just work, but adding this here so others can find this thread.

All 19 comments

Never tested NSwag with .NET Core 2.1, there is also no target for that only NSwagExe_Core20...

Are the middlewares (NSwag.AspNetCore) working?

Yes, both the swagger.json as well as front-end portions work fine.

Looking forward to when NetCore21 can be added. It's now in preview 2 release (I'm having to use it for EF Core functionality).

Studio can load the DLL and find the controllers, but then I get this error:

System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.IAsyncStateMachine' from assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Try running the document in another runtime, e.g. /runtime:NetCore20

I assume with the proper target this will just work, but adding this here so others can find this thread.

Any progress on this?

Hope to add it soon...

Hooray! Looking forward to it!

Ok i managed to fix the ci but i think its compiling with 2.1 preview... can you test with your app?

@RSuter sure, and thanks for getting it done so quickly!

Btw, i didnt test it yet :-)

@RSuter my intent is to generate the swagger.json file and from this, generate client code in C# & Typescript. I would like to do it as seamlessly as possible, so I opted for the msbuild integration.
This is my configuration:

  <Target Name="NSwag" AfterTargets="Build">
    <Copy SourceFiles="@(Reference)" DestinationFolder="$(OutDir)References" />
    <Exec Command="$(NSwagExe_Core21) aspnetcore2swagger /assembly:$(OutDir)$(AssemblyName).dll /output:$(OutDir)swagger.json" />
    <RemoveDir Directories="$(OutDir)References" />
    <!--<Exec Command="$(NSwagExe_Core21) swagger2tsclient /input:$(OutDir)swagger.json /output:..\WebApiSample.Sdk\Scripts\client.ts" />-->
    <!-- other commands omitted -->
  </Target>

At build time vs gives me this error:
_The command " aspnetcore2swagger /assembly:bin\Debugmd5-f17adf1f08d18aaf867c987f86a35289\WebApiSample.dll /output:bin\Debugmd5-f17adf1f08d18aaf867c987f86a35289\swagger.json" exited with code 9009._

When running from the command line, I get a more detailed error.
it looks like a nullref because I'm probably hitting this, thus loading the (non generated) document throws nullref as you can see here:

位 dotnet "C:\Users\Max\.nuget\packages\nswag.msbuild\11.17.4\build\NetCore21/dotnet-nswag.dll" aspnetcore2swagger /assembly:bin\Debug\netcoreapp2.1\WebApiSample.dll /output:swagger.json
NSwag command line tool for .NET Core NetCore21, toolchain v11.17.4.0 (NJsonSchema v9.10.46.0 (Newtonsoft.Json v9.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: C:\Users\Max\.nuget\packages\nswag.msbuild\11.17.4\build\NetCore21
System.NullReferenceException: Object reference not set to an instance of an object.
   at NSwag.SwaggerDocument.FromJsonAsync(String data, String documentPath, SchemaType expectedSchemaType) in C:\projects\nswag\src\NSwag.Core\SwaggerDocument.cs:line 130
   at NSwag.Commands.IsolatedSwaggerOutputCommandBase.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedSwaggerOutputCommandBase.cs:line 36
   at NSwag.Commands.SwaggerGeneration.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\AspNetCore\AspNetCoreToSwaggerCommand.cs:line 166
   at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.Process(String[] args, Object input)
   at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 54

I can successfully generate the client code from a "pre-build" swagger.json at build time, but not generate the swagger.json itself.

Am I missing something?

Ah, I see... I think for now, you have to use the csproj based generator or "aspnet2swagger" with ASP.NET Core 2.1 (also supported)

$(NSwagExe_Core21) aspnetcore2swagger /project:$(ProjectDir)MyProject.csproj /output:$(OutDir)swagger.json"

Yep, it works. Great!! Thanks

So you are now using aspnetcore2swagger /project:?

BTW: It's either /project or /assembly - in the end both should work and output the same :-)

Yes, I'm using aspnetcore2swagger /project the only thing worth mention is that you cannot declare a target that creates the swagger document in the AfterBuild target, otherwise you end up in an infinite loop

Maybe you can add a custom configuration where you don't run the custom target and in the NSwag config you run this custom configuration (Configuration setting I think)

For now I've removed the AfterTarget, so I have to manually execute the target, i.e.dotnet build MyProject.csproj /t:Nswag, but I will look into this configuration thing, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Peter554 picture Peter554  路  3Comments

akamyshanov picture akamyshanov  路  4Comments

molszews picture molszews  路  4Comments

alanedwardes picture alanedwardes  路  3Comments

Zshazz picture Zshazz  路  4Comments