I am trying to use the OpenApi
package in my minimal API project following this tutorial from the Microsoft documentation.
I am using Ubuntu 24.04, and I was able to successfully install the library by running the command:
dotnet add package Microsoft.AspNetCore.OpenApi
The problem comes when i try to use it:
using Microsoft.AspNetCore.OpenApi; /* Error here: The type or namespace name 'OpenApi' does not exist in the namespace 'Microsoft.AspNetCore'*/
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
builder.Services.AddOpenApi(); /* Error here: 'IServiceCollection' does not contain a definition for 'AddOpenApi' and no accessible extension method 'AddOpenApi' accepting a first argument of type 'IServiceCollection' could be found */
app.MapGet("/", () => "Hello World!");
if (app.Environment.IsDevelopment())
{
app.MapOpenApi(); /*Error here: 'WebApplication' does not contain a definition for 'MapOpenApi' and no accessible extension method 'MapOpenApi' accepting a first argument of type 'WebApplication' could be found*/
}
app.Run();
So I am not sure what I am missing, I am using the 9 version and my csproj
file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
</ItemGroup>
</Project>
Any ideas what I am missing?
I am trying to use the OpenApi
package in my minimal API project following this tutorial from the Microsoft documentation.
I am using Ubuntu 24.04, and I was able to successfully install the library by running the command:
dotnet add package Microsoft.AspNetCore.OpenApi
The problem comes when i try to use it:
using Microsoft.AspNetCore.OpenApi; /* Error here: The type or namespace name 'OpenApi' does not exist in the namespace 'Microsoft.AspNetCore'*/
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
builder.Services.AddOpenApi(); /* Error here: 'IServiceCollection' does not contain a definition for 'AddOpenApi' and no accessible extension method 'AddOpenApi' accepting a first argument of type 'IServiceCollection' could be found */
app.MapGet("/", () => "Hello World!");
if (app.Environment.IsDevelopment())
{
app.MapOpenApi(); /*Error here: 'WebApplication' does not contain a definition for 'MapOpenApi' and no accessible extension method 'MapOpenApi' accepting a first argument of type 'WebApplication' could be found*/
}
app.Run();
So I am not sure what I am missing, I am using the .net9 version and my csproj
file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
</ItemGroup>
</Project>
Any ideas what I am missing?
Swagger has been removed in .NET 9, and we should use the following package instead:
dotnet add package Swashbuckle.AspNetCore
And in Program.cs, we should update the code to the following:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/", () => "Hello World!");
app.Run();
The error was happening because I was adding the dependency after building it.
The solution is as simple of moving the AddOpenApi()
before of Build()
method
using Microsoft.AspNetCore.OpenApi; /* Error here: The type or namespace name 'OpenApi' does not exist in the namespace 'Microsoft.AspNetCore'*/
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi(); // move it before building it
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
if (app.Environment.IsDevelopment())
{
app.MapOpenApi(); /*Error here: 'WebApplication' does not contain a definition for 'MapOpenApi' and no accessible extension method 'MapOpenApi' accepting a first argument of type 'WebApplication' could be found*/
}
app.Run();
dotnet add package
does this exact thing already – Michał Turczyn Commented Jan 30 at 8:17