services.AddSwaggerGen() giving error

This happens because the implementation of AddSwaggerGen() extension method in ASP.NET Core requires you to provide Action<SwaggerGenOptions> argument which serves as setup action. For example:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});

You can learn more on how to setup Swagger with ASP.NET Core app here.

UPDATE: In previous versions they had the AddSwaggerGen() extension method accepting no arguments, but this call was accompanied with call ConfigureSwaggerDocument(Action<SwaggerGenOptions> setupAction). I guess they just got rid of ConfigureSwaggerDocument and added setup action to AddSwaggerGen() method. That being said it seems your tutorial shows how to setup obsolete version of the Swagger.


I had problem, that

IServiceCollection does not contain a definition for 'AddSwaggerGen'

I turnes out, that I installed Swashbuckle.AspNetCore.Swagger nuget package instead of Swashbuckle.AspNetCore.

In .NET Core 3, there's some issues as discussed here. The solution is to add the following to the project file, replacing the prior version.

<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc2" />