Unable to resolve service for type ‘Swashbuckle.AspNetCore.Swagger.ISwaggerProvider’

A call to

services.AddSwaggerGen();

appears to be missing in the ConfigureServices.

public static IServiceCollection AddSwaggerGen(
    this IServiceCollection services,
    Action<SwaggerGenOptions> setupAction = null)
{
    // Add Mvc convention to ensure ApiExplorer is enabled for all actions
    services.Configure<MvcOptions>(c =>
        c.Conventions.Add(new SwaggerApplicationConvention()));

    // Register generator and it's dependencies
    services.AddTransient<ISwaggerProvider, SwaggerGenerator>();
    services.AddTransient<ISchemaGenerator, SchemaGenerator>();
    services.AddTransient<IApiModelResolver, JsonApiModelResolver>();

    // Register custom configurators that assign values from SwaggerGenOptions (i.e. high level config)
    // to the service-specific options (i.e. lower-level config)
    services.AddTransient<IConfigureOptions<SwaggerGeneratorOptions>, ConfigureSwaggerGeneratorOptions>();
    services.AddTransient<IConfigureOptions<SchemaGeneratorOptions>, ConfigureSchemaGeneratorOptions>();

    // Used by the <c>dotnet-getdocument</c> tool from the Microsoft.Extensions.ApiDescription.Server package.
    services.TryAddSingleton<IDocumentProvider, DocumentProvider>();

    if (setupAction != null) services.ConfigureSwaggerGen(setupAction);

    return services;
}

which is what adds the ISwaggerProvider to the service collection

You can refactor your current code to

//...

//Configure Swagger
services.AddSwaggerGen(c => { //<-- NOTE 'Add' instead of 'Configure'
    c.SwaggerDoc("v3", new OpenApiInfo {
        Title = "GTrackAPI",
        Version = "v3"
    });
});

//...

which as seen by the source code will eventually call ConfigureSwaggerGen with the setup action.

Leave a Comment