Services.AddSwaggerGen () giving an error

All I'm trying to do is add swagger to the main ASP.Net application. I am looking at a tutorial, and all I see is add services.AddSwaggerGen(); in the configure services area in the Startup.cs file. Like any regular service like MVC ... But I get an error message:

There are no arguments that match the required formal parameter 'setupAction' ...

I don’t see anyone providing any arguments to services.AddSwaggerGen() , so does anyone know what I am missing here?

I added the SwashBuckler.AspNetCore dependency, so the app uses swagger. I just don’t know why it blushes and gives the error above.

+13
source share
2 answers

This is because the implementation of AddSwaggerGen() extension in ASP.NET Core requires the provision of the Action<SwaggerGenOptions> which serves as the installation action. For instance:

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

You can learn more about how to configure Swagger with the ASP.NET Core application here .

UPDATE: In previous versions, they had an AddSwaggerGen() extension method accepting arguments, but this call was followed by a call to ConfigureSwaggerDocument(Action<SwaggerGenOptions> setupAction) . I guess they just got rid of ConfigureSwaggerDocument and added the installation action to AddSwaggerGen() . At the same time, it seems that your lesson shows how to install an outdated version of Swagger.

+5
source

I had a problem that

IServiceCollection does not contain a definition for AddSwaggerGen

It turns out that I installed the Nuget package Swashbuckle.AspNetCore.Swagger instead of Swashbuckle.AspNetCore .

There are some issues in .NET Core 3 that are discussed here . The solution is to add the following to the project file, replacing the previous version.

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

Source: https://habr.com/ru/post/1267329/


All Articles