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(),
-
- public void ConfigureServices(IServiceCollection services)
- {
-
-
-
-
-
- services.AddSwaggerGen();
-
-
-
- }
Additionally, you can modify the information displayed in swagger UI with the following code:
-
- 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!!