AddSwaggerGen not found in .NET 10 means the Swashbuckle package is missing from the project. You add builder.Services.AddSwaggerGen() to an ASP.NET Core API and the build stops with CS1061: ‘IServiceCollection’ does not contain a definition for ‘AddSwaggerGen’. The method does not come from ASP.NET Core. It comes from the Swashbuckle package, and a new .NET 10 Web API project does not reference that package. Below are the two ways out (install Swashbuckle, or use the built-in OpenAPI support), the NU1107 error you can hit right after installing it, and the related errors for AddOpenApi, UseSwaggerUI and OpenApiSecurityScheme.Reference.

AddSwaggerGen not found: the CS1061 error and why it happens
error CS1061: 'IServiceCollection' does not contain a definition for 'AddSwaggerGen' and no accessible
extension method 'AddSwaggerGen' accepting a first argument of type 'IServiceCollection' could be found
(are you missing a using directive or an assembly reference?)
- Swashbuckle.AspNetCore is not installed.
AddSwaggerGen(),UseSwagger()andUseSwaggerUI()are extension methods from that package. - The project came from the .NET 10 Web API template. It references
Microsoft.AspNetCore.OpenApiand callsAddOpenApi()andMapOpenApi(). Tutorials written for Swashbuckle fail here until you add the package. - The package is in another project, such as a class library, and not in the API project that contains Program.cs.
- Only part of Swashbuckle is installed, for example just the SwaggerUI package.
AddSwaggerGen()lives inSwashbuckle.AspNetCore.SwaggerGen.
Fix 1: install Swashbuckle.AspNetCore
Run this in the folder of the project that has Program.cs:
dotnet add package Swashbuckle.AspNetCore
The umbrella package brings in the Swagger, SwaggerGen and SwaggerUI parts, so you do not add them one by one. Then register it:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(); // add AddEndpointsApiExplorer() too if you use minimal APIs
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger(); // serves /swagger/v1/swagger.json
app.UseSwaggerUI(); // serves the UI at /swagger
}
app.MapControllers();
app.Run();
No extra using line is needed for these three methods. On the template project this built, and in Development /swagger/v1/swagger.json returned 200 and /swagger/index.html returned 200.
NU1107 right after installing: version conflict for Microsoft.OpenApi
On a fresh .NET 10 template, dotnet add package Swashbuckle.AspNetCore installed version 11.0.1 and the restore failed:
error NU1107: Version conflict detected for Microsoft.OpenApi. Install/reference Microsoft.OpenApi 3.11.0
directly to project Api to resolve this issue.
Api -> Swashbuckle.AspNetCore 11.0.1 -> Swashbuckle.AspNetCore.Swagger 11.0.1 -> Microsoft.OpenApi (>= 3.11.0)
Api -> Microsoft.AspNetCore.OpenApi 10.0.12 -> Microsoft.OpenApi (>= 2.12.0 && < 3.0.0).
The template package needs Microsoft.OpenApi 2.x while Swashbuckle 11 needs 3.x. Pick one of these:
- Use Swashbuckle for documents: run
dotnet remove package Microsoft.AspNetCore.OpenApiand delete the template’sAddOpenApi()andMapOpenApi()lines. After that the project built and served swagger.json. - Keep the template’s package: install an older Swashbuckle with
dotnet add package Swashbuckle.AspNetCore --version 10.3.1. That combination restored and built on the template.
Fix 2: use the built-in OpenAPI support
If you only want an OpenAPI document, you do not need Swashbuckle at all. The template already has it (OpenAPI support in ASP.NET Core (Microsoft Learn)):
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi(); // serves /openapi/v1.json
}
app.MapControllers();
app.Run();
This produces the JSON but no UI. To keep the Swagger UI page, add only the UI package and point it at the new document. This does not conflict with Microsoft.AspNetCore.OpenApi:
dotnet add package Swashbuckle.AspNetCore.SwaggerUI
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(options =>
options.SwaggerEndpoint("/openapi/v1.json", "My API v1"));
}
I ran this on the template with SwaggerUI 11.0.1: /openapi/v1.json, /swagger/index.html and the UI configuration all returned 200. How to read and export the built-in document is covered in Use the generated OpenAPI documents (Microsoft Learn).
Which one should you choose? For a new project the built-in option has fewer packages. Keep Swashbuckle when you already depend on its filters, such as the ones in IOperationFilter and IDocumentFilter. Differences between the tools are summarised in NSwag vs Swashbuckle.
Related errors: AddOpenApi, UseSwaggerUI and Reference
‘IServiceCollection’ does not contain a definition for ‘AddOpenApi’
error CS1061: 'IServiceCollection' does not contain a definition for 'AddOpenApi' and no accessible
extension method 'AddOpenApi' accepting a first argument of type 'IServiceCollection' could be found
These two come from Microsoft.AspNetCore.OpenApi. After I removed that package from the template, the build failed with CS1061 on both AddOpenApi and MapOpenApi. Add it back with dotnet add package Microsoft.AspNetCore.OpenApi, or switch to Swashbuckle as in Fix 1.
‘IServiceCollection’ does not contain a definition for ‘AddHttpClient’
The same message with a different method name usually has the same kind of cause: the package that defines the extension method is not referenced. AddHttpClient comes from Microsoft.Extensions.Http. In a console project with only Microsoft.Extensions.DependencyInjection, services.AddHttpClient() failed with CS1061 on a ServiceCollection, and dotnet add package Microsoft.Extensions.Http fixed it. Web projects already reference it. I did not test other extension methods such as AddDefaultIdentity.
‘WebApplication’ does not contain a definition for ‘UseSwagger’ or ‘UseSwaggerUI’
Same cause as AddSwaggerGen: the Swashbuckle package (or at least the Swagger and SwaggerUI parts) is missing in this project. The error I got without the package matched the AddSwaggerGen one, with WebApplication as the first argument type.
‘OpenApiSecurityScheme’ does not contain a definition for ‘Reference’
This appears when old JWT-in-Swagger code meets Swashbuckle 10 or later, which uses newer Microsoft.OpenApi types. The reference is now an OpenApiSecuritySchemeReference, and the requirement is built from a function that receives the document. This version compiled and produced the Bearer scheme and a security requirement in swagger.json with Swashbuckle 11.0.1:
using Microsoft.OpenApi;
builder.Services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT"
});
c.AddSecurityRequirement(document => new OpenApiSecurityRequirement
{
[new OpenApiSecuritySchemeReference("Bearer", document)] = []
});
});
Still failing? Quick checklist
- Run
dotnet list packagein the API project and confirm a Swashbuckle package is listed there. - Keep every Swashbuckle package on the same version. Mixed versions cause missing-method errors.
- Run
dotnet restore, then clean and rebuild. In Visual Studio, reopening the solution clears stale IntelliSense. - Check that
TargetFrameworkin the .csproj is what you expect, for examplenet10.0. - If the build works but the UI shows Failed to load API definition, that is a different problem: see Swagger Failed to load API definition.
What was tested and what was not
- Run on macOS with .NET SDK 10.0.401: the CS1061 error on a template project without the package, Fix 1 (Swashbuckle.AspNetCore 11.0.1, build and HTTP 200 for swagger.json and the UI), the NU1107 error and its removal fix, Swashbuckle 10.3.1 on the unmodified template (restore and build only, not run), Fix 2 with SwaggerUI 11.0.1 (run, HTTP 200), the AddOpenApi and MapOpenApi errors, and the security sample (run on 11.0.1).
- Not tested: Visual Studio and the Package Manager Console (the
dotnetcommands do the same work), minimal API projects, Swashbuckle versions between 10.0 and 10.2, other target frameworks, and the security sample on Swashbuckle 10.x.
Package pages: Swashbuckle.AspNetCore on NuGet and Swashbuckle.AspNetCore on GitHub.
Summary
- AddSwaggerGen, UseSwagger and UseSwaggerUI come from Swashbuckle, which the .NET 10 template no longer includes.
- Either add Swashbuckle (remove Microsoft.AspNetCore.OpenApi or use 10.x if NU1107 appears) or use AddOpenApi and MapOpenApi, optionally with only the SwaggerUI package.
- Old JWT code that uses
OpenApiSecurityScheme.Referenceneeds the newer reference type on Swashbuckle 10 and later.
Found this useful?
Bookmark this page and share it with your team. Get new .NET, cloud and database tutorials in your inbox.
AddSwaggerGen not found FAQ
How do I fix ‘IServiceCollection does not contain a definition for AddSwaggerGen’?
Install Swashbuckle.AspNetCore in the project that contains Program.cs with dotnet add package Swashbuckle.AspNetCore, or use the built-in AddOpenApi instead. Swagger itself was not removed from .NET 10; only the project template changed. New Web API projects start with the built-in OpenAPI document and no Swagger UI. Swashbuckle can still be installed as a package.
Where is my swagger.json now?
With Swashbuckle it is at /swagger/v1/swagger.json. With the built-in OpenAPI support it is at /openapi/v1.json. See how to export Swagger JSON or YAML if you need to save the file.
Why does dotnet add package Swashbuckle.AspNetCore fail with NU1107?
Swashbuckle 11 needs Microsoft.OpenApi 3.x, while the template’s Microsoft.AspNetCore.OpenApi package needs 2.x. Remove the template package and its two calls, or install Swashbuckle.AspNetCore 10.3.1 instead.
Did one of these fix your build? Tell us in the comments.
