Introduction

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 Rijsat API",
- Description = "Rijsat ASP.NET Core Web API",
- TermsOfService = new Uri("https://rijsat.com/terms"),
- Contact = new OpenApiContact
- {
- Name = "Rijwan Ansari",
- Email = string.Empty,
- Url = new Uri("https://rijsat.com/spboyer"),
- },
- License = new OpenApiLicense
- {
- Name = "Use under Open Source",
- Url = new Uri("https://rijsat.com/license"),
- }
- });
- });
This modification resolved the issue/error.
Cheers!!

Satya KarkiPosted Feb 1, 2021, 8:50 AM
Helpful blog. Thanks for sharing.