I want to generate the swagger file (OpenAPI specification json file) post building my API. I've added this <Target> XML script in my .csproj file to generate the swagger file but I get the mentioned error message without any description.

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger_v1.json $(OutputPath)$(AssemblyName).dll v1" />
</Target>

But I get the following error:

MSB3073 The command "dotnet swagger tofile --output swagger_v1.json bin\Debug\net6.0\TestSwaggerGenWithDotnetSix.dll v1" exited with code 1.

Tried this stackoverflow source and this source as well but nothing worked out for me.

Tried downgrading the swagger version from 6.5 to 6.4, still getting the same issue.

I also saw some stackoverflow answer where they were writing the swagger file using File.Write during the builder.build event but it fails on server as we don't have file writing permissions in Azure Build Pipeline during Build process.

ISwaggerProvider swaggerService = provider.GetRequiredService<ISwaggerProvider>(); 
OpenApiDocument doc = swaggerService.GetSwagger("v1", null, "/"); 
string swaggerFile = doc.SerializeAsJson(Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0); 
File.WriteAllText("swagger_v1.json", swaggerFile);

My swaggerGenDoc and SwaggerUi are correct as without the above XML script, the project runs smoothly.

builder.Services.AddSwaggerGen(opt =>
{
    opt.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
    {
        Title = "v1",
        Version = "v1"
    });
});

.Net version: 8

Swagger version: Swashbuckle 6.5

0

There are 0 best solutions below