I am running a solution with the following added to the csproj to build a custom swagger document
`
</PropertyGroup>
<Exec Command="dotnet tool restore" />
<Exec Command="dotnet swagger tofile --output myoutputfile myawesomecode.dll v1 " />
`
When I try to run a build the tools restore successfully however the solution starts running instead of writing the documentation. This is an issue because my service needs several other services (eg licensing) to be able to run.
In my company we have another project that does not suffer this issue and seems to have no additional code to prevent it running.
My question is how to create the swagger documentation without the source code running?
After 4 days of searching I found the problem with my code base thanks to this open ticket on the github repository.
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2290
This explained there is an open issue with the swashbuckle CLI and the Minimal Hosting Model that had been already implemented.
My Program.cs looked initially like this:
public class Program
{
public static async Task Main()
{
var builder = WebApplication.CreateBuilder(args);
builder.Service.AddSingleton<ISomeOtherService, SomeOtherService>();
builder.Services.AddSwaggerGen(options =>
//custom swagger gen
);
builder.Services.AddMvc();
//etc
var app = builder.Build();
if (env.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
await app.RunAsync();
}
}
The issue was that the dotnet swagger tofile
code was looking for a class called ‘Startup.cs’ using reflection or some other process to be able to load the swagger documentation.
When Startup.cs is not present then dotnet swagger
runs program main. To confirm this I ran the code twice once with the class called Startup.cs (which ran successfully) and the second time called SomethingElse.cs (which started my application).
To fix the problem, I had to introduce a class which must start with Startup like so:
public class Startup(IConfiguration configuration, IWebHostEnvironment enviroment)
{
public IConfiguration Configuration { get; } = configuration;
public IWebHostEnvironment Environment { get; } = enviroment;
public void ConfigureServices(IServiceCollection services)
{
service.AddSingleton<ISomeOtherService, SomeOtherService>();
services.AddSwaggerGen(options =>
//custom swagger gen
);
services.AddMvc();
//etc
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
}
}
public class Program
{
public static async Task Main()
{
var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration, builder.Environment);
startup.ConfigureServices(builder.Services);
var app = builder.Build();
startup.Configure(app, builder.Environment);
await app.RunAsync();
}
}
On the official documentation for the minimal hosting model the code example references a Startup.cs class as below:
However the swagger documentation makes no mention that NOT having a Startup.cs would cause any issues.