Error while generating swagger.json with Swashbuckle.AspNetCore.Cli v6.4.0
Asked Answered
L

4

5

I've added this rows to my .csproj file

<Target Name="SwaggerPostBuildTarget" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore"></Exec>
    <Exec Command="dotnet tool install Swashbuckle.AspNetCore.Cli --version 6.4.0"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger/v1/swagger.json $(OutputPath)$(AssemblyName).dll v1"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger/v2/swagger.json $(OutputPath)$(AssemblyName).dll v2"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger/v3/swagger.json $(OutputPath)$(AssemblyName).dll v3"></Exec>
</Target>

when I run the project I get this error:

MSB3073 - The command "dotnet swagger tofile --output /swagger/v1/swagger.json bin\Debug\net6.0\xxx.dll v1" exited with code -532462766

I followed step by step the instructions in https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcorecli but it is not working.

Any ideas on what this error means?

UPDATE

With more tries I understood that while executing the post build command the environment variables are not yet set and this is what is causing my error, since I try to read an environment variable. This is my launchsettings.json: enter image description here

This is the output of my execution: enter image description here

What I also notice is that the port on which is listening are not the ones I set, but I can't understand from where they are read

UPDATE 2

I found this: How to get Environment Variable in csproj file? but it is not working and I can't neither get clarification since I cannot comment yet. Also there is this issue on github https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2290 that explain why I'm getting this errors, but still I'm not able to build the project without errors

UPDATE 3 I have been able to generate the json files. Now the instructions in my csproj file looks like this: enter image description here and my Program.cs file is:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Custom configurations

var startup = new Startup(builder.Configuration);

startup.ConfigureServices(builder.Services);

var app = builder.Build();

startup.Configure(app, app.Environment, app.Services.GetRequiredService<IApiVersionDescriptionProvider>());

app.Run();
Lent answered 25/8, 2022 at 9:27 Comment(7)
532462766 could mean whatever. Is there other output information alongside the error description? Try to run "dotnet swagger tofile" command directly in terminal. Does it give more details?Headquarters
I tried but since in the microservice there are some environment variables and some others packages that needs runtime confirgurations it is not easy to run the command in the same conditions. I did some update to the commands since the initial error was that the folder could not be foundLent
Now the command for a single version look like this: <Exec Command="if exist swagger rd /q /s swagger"></Exec> <Exec Command="mkdir swagger"></Exec> <Exec Command="mkdir swagger\v1"></Exec> <Exec Command="dotnet swagger tofile --output swagger/v1/swagger.json $(OutputPath)$(AssemblyName).dll v1"></Exec>Lent
After a lot of tries I have been able to execute the command with the command line and it is working fine. The json file has been generatedLent
But you still have build error?Headquarters
Yes, what I have understood is that while executing the post build command the environment variables are not yet set. This is causing my error, since I try to read the "ASPNETCORE_ENVIRONMENT" variableLent
Now the problem is clear, but I don't get it working even with workaround and adding instructions as specified. Any ideas?Lent
C
7

i needed to reinstall my dotnet tools.

try running this command : dotnet tool restore

Clint answered 26/1, 2023 at 23:1 Comment(0)
B
1

For me it was because two action methods in the controller were sharing the same name "ExampleOne" as demonstrated below

    [HttpPost("example/apiOne", Name = "exampleOne")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public async Task<IActionResult> ActionMethodOne()
    {
        return NoContent();
    }
    
    [HttpPost("example/apiTwo", Name = "exampleOne")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public async Task<IActionResult> ActionMethodTwo()
    {
        return NoContent();
    }
Bandy answered 25/7, 2023 at 9:58 Comment(1)
awesome, in my case it was missing/unspecified routing for httpPost attributeCath
P
1

There is a property to can add that will help you pick up the development settings. It's called EnvironmentVariables.

<Target Name="SwaggerPostBuildTarget" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore"></Exec>
    <Exec Command="dotnet tool install Swashbuckle.AspNetCore.Cli --version 6.4.0"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger/v1/swagger.json $(OutputPath)$(AssemblyName).dll v1" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development"></Exec>
</Target>
Pimp answered 19/1 at 15:35 Comment(0)
U
0

I found another way to get this error if you've set a NUGET_PACKAGES environment variable.

If your nuget packages are not cached in the default location (ie. %userprofile%\.nuget\packages on Windows) then you might see The command "dotnet swagger..." exited with code 1.

I verified that by copying the following folders back into the nuget default location:

  • swashbuckle.aspnetcore
  • swashbuckle.aspnetcore.cli
  • swashbuckle.aspnetcore.swagger
  • swashbuckle.aspnetcore.swaggergen
  • swashbuckle.aspnetcore.swaggerui

That caused the error to go away but I don't recommend that as a fix. Instead, you should probably remove the NUGET_PACKAGES environment variable so the packages are installed (and kept up to date) in their default location.

Undulatory answered 4/1 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.