DEV Community

Wallism
Wallism

Posted on

dotnet swagger tofile : FileNotFoundException dotnet-swagger.xml

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
Enter fullscreen mode Exit fullscreen mode

I encountered this error:

FileNotFoundException: Could not find file 'C:\projectpath\bin\debug\net5.0\dotnet-swagger.xml'
Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Use this:

Assembly.GetAssembly(typeof(ClassInTheCorrectProject)).GetName().Name
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
markjsc profile image
Mark Johnston

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.

Collapse
 
wallism profile image
Wallism

glad it helped :)