Skip to content

Add IServiceProvider support to Cosmos DB extensions - #19349

Merged
Sébastien Ros (sebastienros) merged 4 commits into
microsoft:mainfrom
AdamDotNet:adamvenezia/addServiceProviderSupportCosmosClient
Oct 12, 2026
Merged

Sébastien Ros (sebastienros) merged 4 commits into
microsoft:mainfrom
AdamDotNet:adamvenezia/addServiceProviderSupportCosmosClient

Conversation

@AdamDotNet

@AdamDotNet AdamDotNet commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Add IServiceProvider support to Cosmos DB extensions - co-authored with GPT 5.6-Sol

The benefit would be using the DI container to resolve RequestHandler instances that may require container services to instantiate as part of calling clientOptions.CustomHanndlers.Add(...);

This is a breaking change for 17.0 to change Action<CosmosClientOptions> to Action<IServiceProvider, CosmosClientOptions> for the Cosmos DB integration package.

  • Behavior change - previously, exceptions thrown by Action<CosmosClientOptions>? configureClientOptions would throw upon registration. Now, they will only throw upon client resolution from the DI container.
  • Behavior change - previously, the Builder instance would keep a client in a field as a singleton, now the DI container is used to enforce a singleton because DI is now a requirement to resolve client options. Consumers didn't have access to the private _client previously and won't be able to resolve this CosmosClient from the container due to using an instance as the Keyed registration. This feels like consistent behavior, just a different implementation detail.

Partially addresses #667

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

Testing

Built local packages and pulled them into a real Microsoft internal project. Built and executed, with a breakpoint to watch the clientOptions callback get called. Deleted my internal copy of AspireMicrosoftAzureCosmosExtensions to call the package version added by this PR.

Old overloads still work:
image

New overloads work as expected:
image

Copilot AI balanced review requested due to automatic review settings August 13, 2026 16:22
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 19349

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 19349"

@github-actions github-actions Bot added the area-integrations Issues pertaining to Aspire Integrations packages label Aug 13, 2026

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

Adds DI-aware Cosmos DB client option callbacks, enabling service resolution during client creation.

Changes:

  • Adds IServiceProvider callback overloads.
  • Defers client option configuration until resolution.
  • Adds documentation and tests for legacy, DI-aware, and concurrent scenarios.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
AspireMicrosoftAzureCosmosExtensions.cs Adds overloads and centralizes registration.
CosmosDatabaseBuilder.cs Shares clients through keyed DI.
README.md Documents DI-aware configuration.
AspireMicrosoftAzureCosmosExtensionsTests.cs Tests callbacks and concurrency.
MicrosoftAzureCosmosPublicApiTests.cs Tests overload validation.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@Pilchie

Copy link
Copy Markdown
Member

Tagging Debdatta Kunda (@kundadebdatta) too. This LGTM from the Cosmos DB side of things.

Copilot AI review requested due to automatic review settings August 17, 2026 16:22

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs:291

  • Client creation is now deferred to singleton resolution, but all six public registration methods still document InvalidOperationException as being thrown by the registration call when connection information is missing. The methods now return successfully and that failure (as well as exceptions from configureClientOptions) occurs when the registered service is first resolved, so the IntelliSense contract is inaccurate. Update each overload pair's XML documentation to describe the deferred callback/client creation and resolution-time failure.
        return (settings, serviceProvider =>
        {
            var clientOptions = new CosmosClientOptions();

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@AdamDotNet
AdamDotNet changed the base branch from main to release/14.0 September 22, 2026 17:34
@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@sebastienros
Sébastien Ros (sebastienros) changed the base branch from release/14.0 to main September 23, 2026 15:22
Restore shipped configuration overloads while adding provider-aware options callbacks, retaining registration-time behavior for existing consumers.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 900b109d-3fff-4396-abba-b9e14bb5b877
Breaking API changes are permitted on main for this major release; retain the intended provider-aware Cosmos callback API.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 900b109d-3fff-4396-abba-b9e14bb5b877
@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 900b109d-3fff-4396-abba-b9e14bb5b877
@sebastienros
Sébastien Ros (sebastienros) merged commit 9bdf349 into microsoft:main Oct 12, 2026
54 checks passed
@AdamDotNet
AdamDotNet deleted the adamvenezia/addServiceProviderSupportCosmosClient branch October 12, 2026 21:52
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026
@eerhardt Eric Erhardt (eerhardt) added the breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. label Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-integrations Issues pertaining to Aspire Integrations packages breaking-change Issue or PR that represents a breaking API or functional change over a prerelease.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants