Skip to content

docs: add .NET Aspire singleton CosmosClient pattern#241

Open
AbhiMsft wants to merge 2 commits into
AzureCosmosDB:mainfrom
AbhiMsft:feat/add-aspire-singleton-example
Open

docs: add .NET Aspire singleton CosmosClient pattern#241
AbhiMsft wants to merge 2 commits into
AzureCosmosDB:mainfrom
AbhiMsft:feat/add-aspire-singleton-example

Conversation

@AbhiMsft

Copy link
Copy Markdown

Add platform-specific example for .NET Aspire framework to the SDK singleton best practice rule.

Changes

  • Added Aspire-specific pattern documentation in \sdk-singleton-client.md\
  • Shows incorrect pattern: Multiple \AddKeyedAzureCosmosContainer()\ calls creating separate clients
  • Shows correct pattern: Single \AddAzureCosmosClient()\ + explicit \AddKeyedSingleton\ registration with shared client
  • Includes loop-based registration pattern for clarity and scalability

Impact

Completes singleton rule documentation across all major .NET patterns:

  • ✅ Standard ASP.NET Core DI (ServiceCollectionExtensions)
  • ✅ Azure Functions (Lazy)
  • ✅ .NET Aspire (AddKeyedAzureCosmosContainer)
  • ✅ Rust (Arc)

This addresses a CRITICAL gap where Aspire developers could accidentally create multiple CosmosClient instances, leading to connection pool exhaustion.

…-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
Copilot AI review requested due to automatic review settings July 22, 2026 21:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CosmosClient instances when registering multiple keyed containers.
  • Added a recommended pattern that registers one client and resolves containers via DI using keyed Container registrations.
  • Included a loop-based keyed container registration example and an example of injecting a keyed Container with [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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants