Recently, one of my team member 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

Issue was with swagger generator. Swagger generator was not call or registered in container services of 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 imformation displayed in swagger UI with 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!!

By Rijwan Ansari

Research and Technology Lead | Software Architect | Full Stack .NET Expert | Tech Blogger | Community Speaker | Trainer | YouTuber. Follow me @ https://rijsat.com Md Rijwan Ansari is a high performing and technology consultant with 10 plus years of Software Development and Business Applications implementation using .NET Technologies, SharePoint, Power Platform, Data, AI, Azure and cognitive services. He is also a Microsoft Certified Trainer, C# Corner MVP, Microsoft Certified Data Analyst Associate, Microsoft Certified Azure Data Scientist Associate, CSM, CSPO, MCTS, MCP, with 15+ Microsoft Certifications. He is a research and technology lead in Tech One Global as well as leading Facebook community Cloud Experts Group and SharePoint User Group Nepal. He is a active contributor and speaker in c-sharpcorner.com community, C# Corner MVP and his rank at 20 among 3+ millions members. Additionally, he is knee to learn new technologies, write articles, love to contribute to the open-source community. Visit his blog RIJSAT.COM for extensive articles, courses, news, videos and issues resolution specially for developer and data engineer.

Leave a Reply

Your email address will not be published. Required fields are marked *