Skip to content

Allow users to pass in CosmosClient directly to UseCosmos() #35482

Description

@roji

This tracks adding the ability to pass a CosmosClient directly to UseCosmos(), allowing users to configure their CosmosClient outside of EF Core, with whatever options the Cosmos SDK provides. This removes the current duplication we have around Cosmos configuration options, where we lift and copy Cosmos SDK options to EF's Cosmos options (inevitably falling behind).

Note that the CosmosClient is a singleton service in EF's internal service provider. When a CosmosClient is passed in via the new API, EF will perform reference equality on it. In other words, as long as the same CosmosClient instance is passed in by the user, a single internal service provider will be used; the moment a different instance is passed in, a new service provider will be created by EF. This means that users need to create the CosmosClient instance outside of EF and store it somewhere (e.g. as a singleton DI service or static member), and then pass that instance. If they e.g. instantiate CosmosClient within the DbContext's OnConfiguring, a different instance will be constructed each time; this will quickly trigger EF's "too many service providers" error, letting the user know they're setting things up incorrectly.


Previous design notes

Cosmos supports passing configuration to CosmosClient via CosmosClientOptions. EF doesn't currently allow to be passed directly by the user - we instead wrap those options, exposing EF mechanisms for specifying them instead. Unsurprisingly, new options have been introduced on CosmosClientOptions which we haven't kept up with; there's general consensus in the team this isn't a great story, and that some mechanism for passing CosmosClientOptions to UseCosmos() makes sense.

The main reason EF wraps CosmosClientOptions options is that CosmosClientOptions doesn't currently support value equality, and so cannot be compared in EF's internal service provider cache - see #35481 for a more general discussion of that problem (which also affects other providers). One way forward on that is Azure/azure-cosmos-dotnet-v3#4965, which would add value semantics to CosmosClientOptions, and so we could use our normal caching mechanism as usual (but that wouldn't help the general problem in #35481, e.g. for other providers).

Even if that isn't implemented, we could still add an API accepting CosmosClientOptions, and do one of two things:

  1. Simply ignore CosmosClientOptions in the options caching. As long as users don't vary the configuration (the vast majority?), everything works fine. If users do vary, later options get ignored and the first CosmosClient gets picked up (bad).
  2. Use reference identity for the caching. Everyone using AddDbContext/OnConfiguring will immediately get "too many service providers" if they use AddDbContext/OnConfiguring (since the instance is always different).

Once we introduce such a mechanism, we could also optionally obsolete the current wrapping options, pointing users to the new API instead.

/cc @Pilchie @NinoFloris

Activity

  1. added this to the Backlog milestone on Jan 15, 2025
  2. AndriySvyryd commented on Jan 30, 2025

    @AndriySvyryd
    Member

    Also consider allowing to supply the CosmosClient instance itself, see #26863

  3. Kumima commented on May 22, 2025

    @Kumima

    Can we give this a higher priority? Another benefit this will bring is we can use CosmosClientOptions to enable System.Text.Json.

  4. AndriySvyryd commented on May 22, 2025

    @AndriySvyryd
    Member

    Can we give this a higher priority? Another benefit this will bring is we can use CosmosClientOptions to enable System.Text.Json.

    Unfortunately that will not be the case. To enable STJ we need #14570 and #34567

  5. Kumima commented on May 23, 2025

    @Kumima

    Can we give this a higher priority? Another benefit this will bring is we can use CosmosClientOptions to enable System.Text.Json.

    Unfortunately that will not be the case. To enable STJ we need #14570 and #34567

    Then just please give them higher priority, our release pace is based on year. It's a bit disappointing to wait year after year.

  6. roji commented on May 23, 2025

    @roji
    MemberAuthor

    @Kumima can you please provide a bit more context on why your consider this urgent?

  7. Kumima commented on May 25, 2025

    @Kumima

    @Kumima can you please provide a bit more context on why you consider this urgent?

    All my design for objects to be serialized/deserialized follows the STJ rules. Especially for polymorphism, converters, and name conventions. I think Newtonsoft.Json should have the same functionality, but I don't want to adjust my code to fit Newtonsoft.Json just because EFCore does not support STJ.

  8. AndriySvyryd commented on May 25, 2025

    @AndriySvyryd
    Member

    All my design for objects to be serialized/deserialized follows the STJ rules. Especially for polymorphism, converters, and name conventions. I think Newtonsoft.Json should have the same functionality, but I don't want to adjust my code to fit Newtonsoft.Json just because EFCore does not support STJ.

    EF Core defines its own rules for polymorphism, converters, and name conventions. And they are not affected be the serializer or serialization attributes that you specify on the types. You need to configure the EF model to match your design.

  9. added theissue type on Jun 15, 2025
  10. JoasE commented on Nov 20, 2025

    @JoasE
  11. AndriySvyryd commented on Nov 20, 2025

    @AndriySvyryd
    Member

    EF will create a CosmosClient instance per UseCosmos call.

    This isn't the case. If the configuration passed to the UseCosmos calls is the same the internal CosmosClient instance will be shared between context types.

  12. changed the title [-]Cosmos: Allow users to pass in CosmosClientOptions to UseCosmos()[/-] [+]Cosmos: Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/+] on Mar 29, 2026
  13. self-assigned this
    on Mar 29, 2026
  14. modified the milestones: Backlog, 11.0.0 on Mar 29, 2026
  15. roji commented on Mar 29, 2026

    @roji
    MemberAuthor

    Discussed internally and we're going to allow passing in both a CosmosClient and CosmosClientOptions, performing reference equality on them as proposed above. For the vast majority of users with a single CosmosClient (or a handful), things will just work correctly, with a new service provider for each instance of CosmosClient. Users constantly creating CosmosClient will get our "too many service providers warning"; if their usage is legit, they can suppress the warning.

    Note that we have this same conceptual situation with other non-comparable singleton services such as ILoggerFactory; we document that the same ILoggerFactory instance must be passed in, and show config patterns where it's a static (docs).

  16. changed the title [-]Cosmos: Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/-] [+]Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/+] on Mar 29, 2026
  17. changed the title [-]Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/-] [+]Allow users to pass in CosmosClient directly to UseCosmos()[/+] on Mar 31, 2026
  18. modified the milestones: 11.0.0, Backlog on Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions