Recently, one of my team members faced this error while implementing Swagger in ASP.NET core.

Environment Details

  • Visual Studio 2019
  • ASP.NET Core 3.1
  • Swashbuckle.AspNetCore 5.6.3

Error
Unable to resolve service for type ‘Swashbuckle.AspNetCore.Swagger.ISwaggerProvider’ while attempting to Invoke middleware ‘Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware
 
The issue was with the Swagger generator. The swagger generator was not called or registered in container services of the startup file.
 
Below code: (startup.cs file) add services.AddSwaggerGen(),
    // This method gets called by the runtime. Use this method to add services to the container.  
    public void ConfigureServices(IServiceCollection services)  
    {  
        //...other methods added to servces  
        //...  
        //...  
      
        // Register the Swagger generator, defining 1 or more Swagger documents  
        services.AddSwaggerGen();  
      
        //....other methods added to servces  
        //...  
    }  


Additionally, you can modify the information displayed in swagger UI with the following code:
    // Register the Swagger generator, defining 1 or more Swagger documents  
    services.AddSwaggerGen(c =>  
    {  
        c.SwaggerDoc("v1", new OpenApiInfo  
        {  
            Version = "v1",  
            Title = "My Peter API",  
            Description = "Peter ASP.NET Core Web API",  
            TermsOfService = new Uri("https://Peter.com/terms"),  
            Contact = new OpenApiContact  
            {  
                Name = "Rijwan Ansari",  
                Email = string.Empty,  
                Url = new Uri("https://Peter.com/spboyer"),  
            },  
            License = new OpenApiLicense  
            {  
                Name = "Use under Open Source",  
                Url = new Uri("https://Peter.com/license"),  
            }  
        });  
    });  

This modification resolved the issue/error.

HostForLIFE ASP.NET 3.1.9 Hosting
HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.