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.
No comments:
Post a Comment