Introduction

Unable To Resolve Service For Type ‘Swashbuckle.AspNetCore.Swagger.ISw
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(),
  1. // This method gets called by the runtime. Use this method to add services to the container.
  2. public void ConfigureServices(IServiceCollection services)
  3. {
  4. //...other methods added to servces
  5. //...
  6. //...
  7. // Register the Swagger generator, defining 1 or more Swagger documents
  8. services.AddSwaggerGen();
  9. //....other methods added to servces
  10. //...
  11. }
Additionally, you can modify the information displayed in swagger UI with the following code:
  1. // Register the Swagger generator, defining 1 or more Swagger documents
  2. services.AddSwaggerGen(c =>
  3. {
  4. c.SwaggerDoc("v1", new OpenApiInfo
  5. {
  6. Version = "v1",
  7. Title = "My Rijsat API",
  8. Description = "Rijsat ASP.NET Core Web API",
  9. TermsOfService = new Uri("https://rijsat.com/terms"),
  10. Contact = new OpenApiContact
  11. {
  12. Name = "Rijwan Ansari",
  13. Email = string.Empty,
  14. Url = new Uri("https://rijsat.com/spboyer"),
  15. },
  16. License = new OpenApiLicense
  17. {
  18. Name = "Use under Open Source",
  19. Url = new Uri("https://rijsat.com/license"),
  20. }
  21. });
  22. });
This modification resolved the issue/error.
Cheers!!