I'm trying to use latest version of nswag (11.18.7 at the moment) from msbuild in asp.net core project (netcoreapp2.1) and aspnetcore2swagger command just doesn't work. Error is always System.IO.FileNotFoundException no matter if I use full assembly path, relative path from project folder or just assembly name. I'm using it the way it is recommended in the documentation for .NET Core projects:
<Target Name="NSwag" AfterTargets="Build">
<Copy SourceFiles="@(ReferencePath)" DestinationFolder="$(OutDir)References" />
<Message Importance="high" Text="Run '$(NSwagExe_Core21) aspnetcore2swagger /assembly:$(OutDir)$(AssemblyName).dll'" />
<Exec Command="$(NSwagExe_Core21) aspnetcore2swagger /assembly:$(OutDir)$(AssemblyName).dll" />
<RemoveDir Directories="$(OutDir)References" />
</Target>
If I just replace aspnetcore2swagger command with webapi2swagger without making any other changes - it works perfectly.
Im currently fighting (again) with the assembly loader, hope to find a fix for that soon...
Can you retry with v11.19.0?
Tried with v11.19.0 by replacing webapi2swagger with aspnetcore2swagger in my csproj file but got the same error as with the previous version:
Could not load file or assembly 'C:\Path\To\My\Project\bin\Debug\netcoreapp2.1\bin\Debug\netcoreapp2.1\MyProjectOutputAssembly.dll'.
So it looks like it repeats bin\Debug\netcoreapp2.1 twice unlike webapi2swagger does.
Also tried with a project instead of assembly as you suggested. Here's what I put in my csproj file:
<Target Name="NSwag" AfterTargets="Build">
<Copy SourceFiles="@(ReferencePath)" DestinationFolder="$(OutDir)References" />
<Exec Command="$(NSwagExe_Core21) aspnetcore2swagger /nobuild:true /project:$(MSBuildProjectFile) /output:$(OutDir)swagger.json" />
<RemoveDir Directories="$(OutDir)References" />
</Target>
but then I get this:
System.InvalidOperationException: aspnet2swaggercommand requires the entry point type MyApp.Program to have either an BuildWebHost or CreateWebHostBuilder method. See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x for suggestions on ways to refactor your startup type.
and MyApp.Program does have CreateWebHostBuider method:
public static IWebHostBuilder CreateWebHostBuider(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
Can you provide a sample project to repro this?
I tried to reproduce an error with aspnetcore2swagger and _/project_ parameter and found that I had typo in CreateWebHostBuilder method name (it was manually updated from ASP.NET Core pre 2.1), when I fixed this - everything works.
But I could reproduce error with _/assembly_ parameter. Do you need sample project for this?
There are some problems with .NET Core 2.0 + assembly loading - but with 2.1 it works for my sample project. It would be great to have this failing sample project....
Here's sample project: aspnetcore2swagger.zip demonstrating error with _/assembly_ parameter.
Strangely enough, while _/project_ parameter worked when I built it in Visual Studio, it didn't work in VSTS build and I've got this:
System.InvalidOperationException: Project outputs could not be located in 'D:\a\1\s\src\MyProject\bin\Debug\netcoreapp2.1\'. Ensure that the project has been built.
Here's relevant part of VSTS log: vsts.log
You need to set /nobuild=false
But then I get infinite building loop when I build locally in Visual Studio.
Ok, yes when executed within csproj you need to set nobuild=true, but maybe you need to also change the configuration to release: /configuration=Release because you probably build in Release mode on VSTS?
Assembly load should be fixed with this commit: https://github.com/RSuter/NSwag/commit/c1f8fa998391fe40153d965a34b80d8d1f8164fb
Just releasing v11.19.1 with this fix, please retry with that
Seem to be working fine now. Thanks!
@RSuter I'm getting exactly the same issue as @older had, even with today's new netcore2.2 build. For some reason putting /assembly:RR.API.dll isn't sufficient.
If I run the following command then the nswag command fails:
<Exec Command="$(NSwagExe_Core22) aspnetcore2swagger /documentName:a
/assembly:RR.API.dll /output:$(SpaRoot_APIRoot)rr.api.json" />
System.IO.FileNotFoundException: Could not load file or assembly 'S:\TFS\RR\SPAHost.Web\RR.API.dll'. The system cannot find the file specified.
So clearly it isn't looking in the bin for the DLL. So I tried to add $(OutDir). That gets converted to the following:
/assembly:bin\Debug\netcoreapp2.2\RR.API.dll
However nswag then fails with the following :
System.IO.FileNotFoundException: Could not load file or assembly 'S:\TFS\RR\SPAHost.Web\bin\Debug\netcoreapp2.2\bin\Debug\netcoreapp2.2\RR.API.dll'. The system cannot find the path specified.
As @older said before the string bin\Debug\netcoreapp2.2 is literally doubled. So it's looking for bin\Debug\netcoreapp2.2\bin\Debug\netcoreapp2.2 which obviously doesn't exist.
Workaround for now. Putting $(ProjectDir)\bin\debug\netcoreapp2.2
<Exec Command="$(NSwagExe_Core22) aspnetcore2swagger /documentName:a
/assembly:$(ProjectDir)bin\Debug\netcoreapp2.2\RR.API.dll /output:$(SpaRoot_APIRoot)rr.api.json" />
Thanks for the 2.2 fix. It's really nice now to be back to just hitting F5 and having my Angular app refresh its API :-)
@RSuter Can confirm that I'm getting the same problem again when upgrading from 11.20.1 to 12.0.7. If I revert back to v11 - the problem disappear.
Same here with the "bin\Debug\netcoreapp2.1" doubled. And even when I made it find the assembly somehow it halted with "Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration'" exception.
Then I literally copied the recommended snippet from here which uses nswag.json instead of command line parameters and all the trouble magically went away.
Workaround for now. Putting
$(ProjectDir)\bin\debug\netcoreapp2.2
@simeyla, can you help me with where and how you defined ProjectDir variable? - Or is it already a canned variable? When I use it I get an error System.UnauthorizedAccessException: Access to the path 'C:\Users\espressobeans\Application Data' is denied.
I was able to figure out the UnauthorizedAccessException error, it had to deal with the fact that my local repo was in Microsoft OneDrive. This is what works for me:
<Exec Command="$(NSwagExe_Core22) aspnetcore2swagger /assembly:$(ProjectDir)bin\Release\netcoreapp2.2\$(AssemblyName).dll /output:$(OutDir)swagger.json" />
However, it would be better if I could use $(OutDir) instead of $(ProjectDir).
I've tried debugging this issue, but I got stuck at IsolatedCommandBase.RunIsolatedAsync, unable to step into the Task.Run code.
But I also found a satisfactory workaround by using $(OutDir) to set the working directory instead of passing it to /assembly::
<Exec Command="$(NSwagExe_Core21) aspnetcore2swagger /assembly:$(TargetFileName) /output:build-artifacts/swagger.json" WorkingDirectory="$(OutDir)" />