docs: add .NET Aspire singleton CosmosClient pattern#241
Open
AbhiMsft wants to merge 2 commits into
Open
Conversation
…-client rule - Add platform-specific example for .NET Aspire (AddKeyedAzureCosmosContainer) - Show incorrect pattern (multiple separate clients) vs. correct (one shared client) - Include loop-based registration pattern for clarity and maintainability - Completes singleton rule documentation across .NET DI, Azure Functions, and Aspire
AbhiMsft
requested review from
TheovanKraay,
jaydestro and
sajeetharan
as code owners
July 22, 2026 21:23
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Cosmos DB best-practices skill documentation to include a .NET Aspire-specific example for the “singleton CosmosClient” guidance, helping Aspire apps avoid creating multiple client instances when registering multiple containers.
Changes:
- Added a .NET Aspire example demonstrating an anti-pattern that can lead to multiple
CosmosClientinstances when registering multiple keyed containers. - Added a recommended pattern that registers one client and resolves containers via DI using keyed
Containerregistrations. - Included a loop-based keyed container registration example and an example of injecting a keyed
Containerwith[FromKeyedServices].
Comment on lines
+197
to
+210
| var builder = Host.CreateApplicationBuilder(); | ||
| builder.AddAzureCosmosClient("cosmosdb"); | ||
|
|
||
| // Adding multiple keyed containers this way can create separate clients internally | ||
| builder.AddKeyedAzureCosmosContainer("orders"); | ||
| builder.AddKeyedAzureCosmosContainer("products"); | ||
| builder.AddKeyedAzureCosmosContainer("customers"); | ||
|
|
||
| // ✅ CORRECT (one shared client, explicit container references): | ||
| var builder = Host.CreateApplicationBuilder(); | ||
|
|
||
| // Register single CosmosClient | ||
| var cosmosClient = builder.AddAzureCosmosClient("cosmosdb"); | ||
|
|
Use 'correctBuilder' in the correct section to avoid variable redeclaration that would cause compilation error. Addresses Copilot review feedback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add platform-specific example for .NET Aspire framework to the SDK singleton best practice rule.
Changes
Impact
Completes singleton rule documentation across all major .NET patterns:
This addresses a CRITICAL gap where Aspire developers could accidentally create multiple CosmosClient instances, leading to connection pool exhaustion.