Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 245
Emanuele Avatar asked Sep 08 '26 14:09

Emanuele


2 Answers

i needed to reinstall my dotnet tools.

try running this command : dotnet tool restore

like image 114
Max Alexander Hanna Avatar answered Sep 11 '26 21:09

Max Alexander Hanna


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();
    }
like image 29
Joshua Duxbury Avatar answered Sep 11 '26 23:09

Joshua Duxbury