3

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();
Emanuele
  • 73
  • 3
  • 11
  • 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? – BorisR Aug 25 '22 at 12:13
  • 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 found – Emanuele Aug 25 '22 at 15:26
  • Now the command for a single version look like this: ` ` – Emanuele Aug 25 '22 at 15:27
  • 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 generated – Emanuele Aug 26 '22 at 07:56
  • But you still have build error? – BorisR Aug 26 '22 at 14:13
  • 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" variable – Emanuele Aug 29 '22 at 13:44
  • Now the problem is clear, but I don't get it working even with workaround and adding instructions as specified. Any ideas? – Emanuele Sep 05 '22 at 13:58

2 Answers2

0

i needed to reinstall my dotnet tools.

try running this command : dotnet tool restore

Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35
0

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();
    }
Joshua Duxbury
  • 4,892
  • 4
  • 32
  • 51