DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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 (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.