Repository navigation
Description
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:
- 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).
- 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
AndriySvyryd commentedon Jan 30, 2025
Also consider allowing to supply the
CosmosClientinstance itself, see #26863Kumima commentedon May 22, 2025
Can we give this a higher priority? Another benefit this will bring is we can use
CosmosClientOptionsto enableSystem.Text.Json.AndriySvyryd commentedon May 22, 2025
Unfortunately that will not be the case. To enable STJ we need #14570 and #34567
Kumima commentedon May 23, 2025
Then just please give them higher priority, our release pace is based on year. It's a bit disappointing to wait year after year.
roji commentedon May 23, 2025
@Kumima can you please provide a bit more context on why your consider this urgent?
Kumima commentedon May 25, 2025
All my design for objects to be serialized/deserialized follows the STJ rules. Especially for polymorphism, converters, and name conventions. I think
Newtonsoft.Jsonshould have the same functionality, but I don't want to adjust my code to fitNewtonsoft.Jsonjust because EFCore does not support STJ.AndriySvyryd commentedon May 25, 2025
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.
JoasE commentedon Nov 20, 2025
AndriySvyryd commentedon Nov 20, 2025
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.
[-]Cosmos: Allow users to pass in CosmosClientOptions to UseCosmos()[/-][+]Cosmos: Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/+]roji commentedon Mar 29, 2026
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).
[-]Cosmos: Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/-][+]Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/+][-]Allow users to pass in CosmosClient and CosmosClientOptions to UseCosmos()[/-][+]Allow users to pass in CosmosClient directly to UseCosmos()[/+]