Swashbuckle.AspNetCore.Cli not working with appsettings.json
Asked Answered
A

2

7

It seems I cannot get Swashbuckle.AspNetCore.Cli to work with my .NET Core 3.1 project. I've installed the Cli using the these instruction and building the project works fine. But whenever I try to generate the swagger.json it gives me unhandled exceptions related to appsettings.json variables which cannot be found. It seems the running the assembly with the Cli tool doesn't load my settings. Has someone else run into this problem before?

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
Aneroidograph answered 8/2, 2021 at 19:58 Comment(5)
When the CLI runs, it does not run program.cs but via reflection runs your startup class. It makes for weird scenarios where you may need special handling/logic to pull in the settings/configuration u are setting in program.cs via the configuration delegatesLitterbug
@Litterbug Thanks for the feedback, any idea how I might go about doing that?Aneroidograph
You can test the current program name, as it will be running as the cli's Exe name and then setup whatever is missing. If memory serves you can use PlatformServices.Default.Application.ApplicationNameLitterbug
I am having the same issue. If I run on Windows, it works. But if I run on Linux (on pipeline or WSL) I can't read appsettings values. And I am using a SwaggerWebHostFactory.Gasholder
Did you achieve your goal? I have exactly same issue. Any help will be appreciate .Psychrometer
H
2

If the purpose is for generation outside the code and various issues are preventing it from loading correctly, you could create another entrypoint with the simplest version of the application to provide what the swagger generator needs like this:

public static class SwaggerGen
{
    public static bool EntryPointForSwaggerGenerationApplication(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        if (Assembly.GetEntryAssembly()?.GetName().Name == "dotnet-swagger")
        {
            // Add services to the container.
            builder.Services.AddControllers();
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var appForGen = builder.Build();
            appForGen.UseSwagger();
            appForGen.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", Names.ApplicationName);
            });

            appForGen.Run();
            return true;
        }
        return false;
    }
}

Then the start of your program could look like this:

if (SwaggerGen.EntryPointForSwaggerGenerationApplication(args))
    return;

This will allow swagger generation to run from dotnet swagger tofile and only enter this small application that allows it to access what it needs.

Hartsell answered 10/3, 2023 at 11:2 Comment(1)
Similarly it can be useful to know that if you use NSwag for client generation for example with an MSBuild Task you should also check for the entry assembly: NSwag.AspNetCore.LauncherHartsell
I
0

I ran into this with .Net 6 and Swashbuckle 6.2.3. I had to check for the entry assembly, as suggested in the comments:

AppConfig? configuration = null;
var builder = WebApplication.CreateBuilder(args);

if (Assembly.GetEntryAssembly()?.GetName().Name != "dotnet-swagger")
{
    // Had only this line previously
    builder.Host.ConfigureAppConfiguration(config => configuration = config.Build().Get<AppConfig>());
}
else
{
    configuration = new AppConfig { /* Configure manually for Swagger's use */ };
}
Implausible answered 30/12, 2022 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.