Skip to content

Commit e8205f8

Browse files
authored
Fix IOpenApiDocumentProvider injection sample (keyed DI) (#36426)
1 parent 6086e2c commit e8205f8

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

aspnetcore/fundamentals/openapi/using-openapi-documents.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to use OpenAPI documents in an ASP.NET Core app.
55
ms.author: safia
66
monikerRange: '>= aspnetcore-6.0'
77
ms.custom: mvc
8-
ms.date: 12/04/2025
8+
ms.date: 01/13/2026
99
uid: fundamentals/openapi/using-openapi-documents
1010
---
1111
# Use openAPI documents
@@ -65,8 +65,8 @@ Enable document generation at build time by setting the following properties in
6565

6666
```xml
6767
<PropertyGroup>
68-
<OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)</OpenApiDocumentsDirectory>
69-
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
68+
<OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)</OpenApiDocumentsDirectory>
69+
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
7070
</PropertyGroup>
7171
```
7272

@@ -102,34 +102,45 @@ The output shows any issues with the OpenAPI document. For example:
102102

103103
## Support for injecting `IOpenApiDocumentProvider`
104104

105-
You can inject <xref:Microsoft.AspNetCore.OpenApi.IOpenApiDocumentProvider> into your services through dependency injection to access OpenAPI documents programmatically, even outside HTTP request contexts.
105+
Inject <xref:Microsoft.AspNetCore.OpenApi.IOpenApiDocumentProvider> into services to access OpenAPI documents programmatically, even outside HTTP request contexts. The following example customizes version 2 ("`v2`") of the document with title, version, and description information:
106106

107107
```csharp
108-
public class CustomDocumentService
109-
{
110-
private readonly IOpenApiDocumentProvider _documentProvider;
108+
using Microsoft.AspNetCore.OpenApi;
109+
using Microsoft.OpenApi;
111110

112-
public CustomDocumentService(IOpenApiDocumentProvider documentProvider)
111+
public class CustomDocumentService(
112+
[FromKeyedServices("v2")] IOpenApiDocumentProvider documentProvider)
113+
{
114+
public async Task<OpenApiDocument> GetApiDocumentAsync(
115+
CancellationToken cancellationToken = default)
113116
{
114-
_documentProvider = documentProvider;
115-
}
117+
var document =
118+
await documentProvider.GetOpenApiDocumentAsync(cancellationToken);
116119

117-
public async Task<OpenApiDocument> GetApiDocumentAsync()
118-
{
119-
return await _documentProvider.GetOpenApiDocumentAsync("v1");
120+
document.Info = new OpenApiInfo
121+
{
122+
Title = "Custom API Title",
123+
Version = "v2",
124+
Description = "This is a custom API description for version 2."
125+
};
126+
127+
return document;
120128
}
121129
}
122130
```
123131

124-
Register the service in your DI container:
132+
Register the service in your DI container. Note that service key should match the document name passed to <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A>:
125133

126134
```csharp
135+
builder.AddOpenApi(); // Adds "v1" by default
136+
builder.AddOpenApi("v2");
127137
builder.Services.AddScoped<CustomDocumentService>();
128138
```
129139

130140
This enables scenarios such as generating client SDKs, validating API contracts in background processes, or exporting documents to external systems.
131141

132142
Support for injecting `IOpenApiDocumentProvider` was introduced in ASP.NET Core in .NET 10. For more information, see [dotnet/aspnetcore #61463](https://github.com/dotnet/aspnetcore/pull/61463).
143+
133144
:::moniker-end
134145

135146
[!INCLUDE[](~/fundamentals/openapi/includes/using-openapi-documents-6-8.md)]

0 commit comments

Comments
 (0)