The Problem
Trying to generate swagger from the compiled dll using this command with the swagger CLI:
dotnet swagger tofile --output "swagger-output.json" "C:\projectpath\bin\debug\net5.0\project.dll" v1
I encountered this error:
FileNotFoundException: Could not find file 'C:\projectpath\bin\debug\net5.0\dotnet-swagger.xml'
The suspicious thing here is the name of the xml file it is looking for, it should be looking for my projects xml file, not dotnet-swagger.xml!
This getting started tutorial has some code that causes this problem...when it loads the xml comments it does this to get the assembly:
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
This works fine for most(all?) other use cases but when trying to generate the swagger using the CLI the executing assembly is dotnet-swagger.
The Fix
Instead of:
Assembly.GetExecutingAssembly().GetName().Name
Use this:
Assembly.GetAssembly(typeof(ClassInTheCorrectProject)).GetName().Name
Other Possible Causes
Make sure the xml file is being created in the same folder as the dll and your generate command is passing the correct path.
Top comments (2)
I'm late to the party... MANY THANKS FOR THIS!! I'm not sure I would have tracked this down without your post!
It makes perfect sense, but I didn't question it during Swashbuckle/Swagger generation since it works as expected at runtime.
Thanks again for taking time to share.
glad it helped :)