Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Info' class not recognized with Swashbuckle and Swagger libraries

Am I missing a package or reference because my AddSwaggerGen method does not compile. My code:

I added using Swashbuckle.AspNetCore.Swagger;

but this code

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

will not compile:

Error CS0246 The type or namespace name 'Info' could not be found (are you missing a using directive or an assembly reference?) Here is the csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IdentityModel" Version="3.10.6" />
    <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc2" />
    <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc2" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc2" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc2" />
  </ItemGroup>

like image 287
user1633146 Avatar asked Jun 08 '19 02:06

user1633146


People also ask

What is swashbuckle and Swagger?

Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc. AspNetCore.

What is AddSwaggerGen?

AddSwaggerGen is an extension method to add swagger services to the collection. To configure Swagger, you invoke the method SwaggerDoc. Passing an Info object, you can define the title, description, contact information, and more in code file Startup. cs. Next open Startup.

What is documentName in Swagger?

The {documentName} refers to the name you specify in the AddSwaggerGen() method. The following code uses myapi as the name for a swagger document. builder.Services.AddSwaggerGen(options => options.SwaggerDoc("myapi", new OpenApiInfo { Title = "My API", Version = "v1" }) );


2 Answers

Info is deprecated in version 5

It has been replaced with OpenApiInfo so you have to reflect that in your swagger setup:

c.SwaggerDoc("My_Api", new OpenApiInfo { Version = "1.0", Description = "my api " });
like image 55
Amr Rashed Avatar answered Sep 17 '22 15:09

Amr Rashed


Change Info class to OpenApiInfo class.

add using Microsoft.OpenApi.Models

like image 24
Imma Avatar answered Sep 19 '22 15:09

Imma