IServiceCollection doesn’t contain a definition of AddSwaggerGen
Issue Description
Swagger doesn’t recognize AddSwaggerGen after adding swagger NuGet package i.e Swashbuckle.
IServiceCollection’ does not contain a definition for ‘AddSwaggerGen’ and no accessible extension method ‘AddSwaggerGen’ accepting a first argument of type ‘IServiceCollection’ could be found
Resolution
The issue is commonly produced due to wrong Nuget packages references used in the application.
Kindly add the correct package as below.
Add Swashbuckle.AspNetCore NuGet package
Please add the below Nuget package to your API using a Command prompt or PMC(package manager console)
PM> Install-Package Swashbuckle.AspNetCore -Version 5.3.3
Note: Please try to use the latest available version.
This NuGet package shall add all other required components as shown below,
- Swashbuckle.AspNetCore.SwaggerUI
- Swashbuckle.AspNetCore.Swagger
- Swashbuckle.AspNetCore.SwaggerGen
Above single packages take care of all UI and swagger middleware components installation issues. You need not have to add those packages individually.
Once you compile the project, it works fine and generates the Swagger documentation.
References:
- SwashBuckle – Swagger OpenAPI Documentation in ASP.NET Core
- NSwag Swagger API documentation in ASP.NET Core
Did the above steps resolve your issue? Please sound off your comments below!
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.