Swagger will not generate swagger.json file although the AddSwaggerGen method is present in my Programs.cs file and it prevents me from publishing to Azure App Services

Troy Crowe 5 Reputation points
2023-12-06T07:26:03.05+00:00

I have noticed this is a big issue for many and it is stopping me from getting deployed to Azure. I have two APIs and a website and I am building a PaaS integration. This issue is keeping me from getting my applications migrated and my APIs will not work because of it. Anyway, I get the error listed below when attempting to Publish to my App Service. As we all know there is not a StartUp.cs file with a .net 8 Web Api. The application works and runs great locally, but I cannot get it to communicate between the Web App and the Web APIs. I successfully log in using B2C user flows. Everything is configured using connected services connection so I have all secret information in Key Vault. I am also using Azure Storage, and Azure API Manangement. I have been stuck on this for 3 days and because of that I have upgraded my support subscription to Standard. Anyway, I need help. Thanks.

Failed to generate swagger file. Error dotnet swagger tofile --serializeasv2 --output "C:\Users\TroyC\Source\repos\EMSAPI\bin\Release\net8.0\swagger.json" "C:\Users\TroyC\Source\repos\EMSAPI\bin\Release\net8.0\EMSAPI.dll" v1

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Swashbuckle.AspNetCore.Swagger, Version=6.5.0.0, Culture=neutral, PublicKeyToken=62657d7474907593'. The system cannot find the file specified.
File name: 'Swashbuckle.AspNetCore.Swagger, Version=6.5.0.0, Culture=neutral, PublicKeyToken=62657d7474907593'
   at Swashbuckle.AspNetCore.Cli.Program.<>c.<main>b__0_4(IDictionary`2 namedArgs)
   at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\CommandRunner.cs:line 68
   at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\CommandRunner.cs:line 59
   at Swashbuckle.AspNetCore.Cli.Program.Main(String[] args) in C:\projects\ahoy\src\Swashbuckle.AspNetCore.Cli\Program.cs:line 121
Be sure that the Startup.cs for your application is calling AddSwaggerGen from within ConfigureServices in order to generate swagger file. Visit https://go.microsoft.com/fwlink/?LinkId=2131205&CLCID=0x409 for more information.
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,371 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,405 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,341 questions
{count} vote

2 answers

Sort by: Most helpful
  1. ajkuma 24,661 Reputation points Microsoft Employee
    2023-12-08T18:26:14.0166667+00:00

    Thanks for posting this question.

    If you haven't tried this already, try the steps below to isolate the issue further.

    1. Create a new publish profile and republish the application.
    2. Check the published file path, make sure the path or folder name not contains the spaces.
    3. Install the Swashbuckle.AspNetCore.SwaggerGen package.

    Ref: discussion thread

    I have also added additional relevant tags to receive insights from the targetted audience/SMEs.

    Kindly let us know, I'll follow-up with you further.

    0 comments No comments

  2. aMan 0 Reputation points
    2024-03-21T03:59:41.3266667+00:00

    I had the same problem where it ran fine locally and refused to publish with above error message. Eventually I figured out that the connection string was wrong (In my case the password was off). It ran locally because the connection string was set differently in the debug environment.

    Not sure what that has to do with failing to generate a Swagger file, maybe someone with more knowledge can add that piece of info.

    0 comments No comments