dotnet swagger tofile : FileNotFoundException dotnet-swagger.xml

wallism

Wallism

Posted on November 3, 2021

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.

💖 💪 🙅 🚩
wallism
Wallism

Posted on November 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related