docs: add .NET Aspire singleton CosmosClient pattern - #241
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
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].
| 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.
Sajeetharan (sajeetharan)
left a comment
There was a problem hiding this comment.
Thanks for adding the .NET Aspire guidance. This needs changes before merging because the proposed “correct” example does not compile: AddAzureCosmosClient returns void, so its result cannot be assigned to cosmosClient.
Aspire already provides the supported shared-client pattern:
Aspire’s tests confirm these containers share the same CosmosClient. Please replace the manual DI registrations with this official pattern, avoid registering each keyed container twice, add an Aspire documentation reference, and bump the package version.
npm run validate passes, but compiling the submitted example fails with CS0815; the official builder pattern compiles successfully.> builder.AddAzureCosmosDatabase("cosmosdb")
.AddKeyedContainer("orders")
.AddKeyedContainer("products")
.AddKeyedContainer("customers");
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.