rahul
rahul

Reputation: 294

Swagger error on .net core 3.0 webapi on windows (Unable to resolve service for type 'ISwaggerProvider' )

I have this default Web APi scaffolding . i included following Nuget packages. i have the following file in Startup.cs. however it is throwing error. please assist

nuget packages installed error on screen

My Startup.cs class has the following:-

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = "v1";
                var basePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
                c.SwaggerEndpoint($"{basePath}/swagger/{c.RoutePrefix}/swagger.json", "Name");
            });

Upvotes: 0

Views: 3387

Answers (1)

cl0ud
cl0ud

Reputation: 1614

The reason why you are having this problem is because you are not calling services.AddSwaggerGen() in your ConfigureServices(IServiceCollection services) method.

This method registers the ISwaggerProvider interface in the DI container , since it isn't registered you are receiving this exception when invoking your swagger route.

Full example of swagger section in ConfigureServices :

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });

Upvotes: 1

Related Questions