Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer possible to initialize CosmosDB client #28018

Open
maximecaron opened this issue May 12, 2022 · 2 comments
Open

No longer possible to initialize CosmosDB client #28018

maximecaron opened this issue May 12, 2022 · 2 comments

Comments

@maximecaron
Copy link

maximecaron commented May 12, 2022

File a bug

In Microsoft.Azure.Cosmos v3.27.0 to initialize cosmosdb the only way is now to call CosmosClient.CreateAndInitializeAsync();
With CosmosSDK 2.0 it was possible to call OpenAsync to avoid startup latency on first request Ex:

await context.DataBase.GetCosmosclient().OpenAsync();

But this method have been removed and it's no possible anymore to Initialize CosmosDB client after it have been created.
The problem we are having with Entity Framework Core is that Entity Framework is creating the CosmosClient instance at

: new CosmosClient(_connectionString, _options);

And is not calling CreateAndInitializeAsync()

This is causing first request to take 2 seconds instead of 20 ms

@AndriySvyryd AndriySvyryd changed the title No longer possible to CosmosDB client No longer possible to initialize CosmosDB client May 13, 2022
@AndriySvyryd
Copy link
Member

Since it's an async method adding new API would be non-trivial and therefore we first need to see whether there is more interest in this.

As a workaround you can run a small query instead of context.DataBase.GetCosmosclient().OpenAsync();

@AndriySvyryd AndriySvyryd added this to the Backlog milestone May 13, 2022
@maximecaron
Copy link
Author

maximecaron commented Nov 30, 2023

@AndriySvyryd One suggestion would be to change SingletonCosmosClientWrapper.cs to

    public virtual Task<CosmosClient> Client
        => _client ??= string.IsNullOrEmpty(_connectionString)
            ? CosmosClient.CreateAndInitializeAsync(_endpoint, _key, _options)
            : CosmosClient.CreateAndInitializeAsync(_connectionString, _options);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants