Share via


CosmosClient.CreateAndInitializeAsync Method

Definition

Overloads

CreateAndInitializeAsync(String, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Creates a new CosmosClient with the account endpoint URI string and TokenCredential. In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

CreateAndInitializeAsync(String, AzureKeyCredential, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Creates a new CosmosClient with the account endpoint URI string and AzureKeyCredential. AzureKeyCredential enables changing/updating master-key/ResourceToken while CosmosClient is still in use.

In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

CreateAndInitializeAsync(String, TokenCredential, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Creates a new CosmosClient with the account endpoint URI string and TokenCredential. In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

CreateAndInitializeAsync(String, String, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Creates a new CosmosClient with the account endpoint URI string and TokenCredential. In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

CreateAndInitializeAsync(String, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Source:
CosmosClient.cs

Creates a new CosmosClient with the account endpoint URI string and TokenCredential. In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

public static System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient> CreateAndInitializeAsync (string connectionString, System.Collections.Generic.IReadOnlyList<(string databaseId, string containerId)> containers, Microsoft.Azure.Cosmos.CosmosClientOptions cosmosClientOptions = default, System.Threading.CancellationToken cancellationToken = default);
static member CreateAndInitializeAsync : string * System.Collections.Generic.IReadOnlyList<ValueTuple<string, string>> * Microsoft.Azure.Cosmos.CosmosClientOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient>
Public Shared Function CreateAndInitializeAsync (connectionString As String, containers As IReadOnlyList(Of ValueTuple(Of String, String)), Optional cosmosClientOptions As CosmosClientOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of CosmosClient)

Parameters

connectionString
String

The connection string to the cosmos account. ex: AccountEndpoint=https://XXXXX.documents.azure.com:443/;AccountKey=SuperSecretKey;

containers
IReadOnlyList<ValueTuple<String,String>>

Containers to be initialized identified by it's database name and container name.

cosmosClientOptions
CosmosClientOptions

(Optional) client options

cancellationToken
CancellationToken

(Optional) Cancellation Token

Returns

A CosmosClient object.

Examples

The CosmosClient is created with the ConnectionString and 2 containers in the account are initialized

using Microsoft.Azure.Cosmos;
List<(string, string)> containersToInitialize = new List<(string, string)>
{ ("DatabaseName1", "ContainerName1"), ("DatabaseName2", "ContainerName2") };

CosmosClient cosmosClient = await CosmosClient.CreateAndInitializeAsync("connection-string-from-portal",
                                                                        containersToInitialize)

// Dispose cosmosClient at application exit

Remarks

Emulator: To ignore SSL Certificate please suffix connectionstring with "DisableServerCertificateValidation=True;". When CosmosClientOptions.HttpClientFactory is used, SSL certificate needs to be handled appropriately. NOTE: DO NOT use this flag in production (only for emulator)

Applies to

CreateAndInitializeAsync(String, AzureKeyCredential, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Source:
CosmosClient.cs

Creates a new CosmosClient with the account endpoint URI string and AzureKeyCredential. AzureKeyCredential enables changing/updating master-key/ResourceToken while CosmosClient is still in use.

In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

public static System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient> CreateAndInitializeAsync (string accountEndpoint, Azure.AzureKeyCredential authKeyOrResourceTokenCredential, System.Collections.Generic.IReadOnlyList<(string databaseId, string containerId)> containers, Microsoft.Azure.Cosmos.CosmosClientOptions cosmosClientOptions = default, System.Threading.CancellationToken cancellationToken = default);
static member CreateAndInitializeAsync : string * Azure.AzureKeyCredential * System.Collections.Generic.IReadOnlyList<ValueTuple<string, string>> * Microsoft.Azure.Cosmos.CosmosClientOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient>
Public Shared Function CreateAndInitializeAsync (accountEndpoint As String, authKeyOrResourceTokenCredential As AzureKeyCredential, containers As IReadOnlyList(Of ValueTuple(Of String, String)), Optional cosmosClientOptions As CosmosClientOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of CosmosClient)

Parameters

accountEndpoint
String

The cosmos service endpoint to use

authKeyOrResourceTokenCredential
AzureKeyCredential

AzureKeyCredential with master-key or resource token.

containers
IReadOnlyList<ValueTuple<String,String>>

Containers to be initialized identified by it's database name and container name.

cosmosClientOptions
CosmosClientOptions

(Optional) client options

cancellationToken
CancellationToken

(Optional) Cancellation Token

Returns

A CosmosClient object.

Examples

The CosmosClient is created with the AccountEndpoint, AccountKey or ResourceToken and 2 containers in the account are initialized

using Microsoft.Azure.Cosmos;
List<(string, string)> containersToInitialize = new List<(string, string)>
{ ("DatabaseName1", "ContainerName1"), ("DatabaseName2", "ContainerName2") };

AzureKeyCredential keyCredential = new AzureKeyCredential("account-master-key/ResourceToken");
CosmosClient cosmosClient = await CosmosClient.CreateAndInitializeAsync("account-endpoint-from-portal", 
                                                                        keyCredential,
                                                                        containersToInitialize)

....

// To udpate key/credentials 
keyCredential.Update("updated master-key/ResourceToken");

// Dispose cosmosClient at application exit

Remarks

AzureKeyCredential enables changing/updating master-key/ResourceToken whle CosmosClient is still in use.

Applies to

CreateAndInitializeAsync(String, TokenCredential, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Source:
CosmosClient.cs

Creates a new CosmosClient with the account endpoint URI string and TokenCredential. In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

public static System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient> CreateAndInitializeAsync (string accountEndpoint, Azure.Core.TokenCredential tokenCredential, System.Collections.Generic.IReadOnlyList<(string databaseId, string containerId)> containers, Microsoft.Azure.Cosmos.CosmosClientOptions cosmosClientOptions = default, System.Threading.CancellationToken cancellationToken = default);
static member CreateAndInitializeAsync : string * Azure.Core.TokenCredential * System.Collections.Generic.IReadOnlyList<ValueTuple<string, string>> * Microsoft.Azure.Cosmos.CosmosClientOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient>
Public Shared Function CreateAndInitializeAsync (accountEndpoint As String, tokenCredential As TokenCredential, containers As IReadOnlyList(Of ValueTuple(Of String, String)), Optional cosmosClientOptions As CosmosClientOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of CosmosClient)

Parameters

accountEndpoint
String

The cosmos service endpoint to use.

tokenCredential
TokenCredential

TokenCredentialThe token to provide AAD token for authorization.

containers
IReadOnlyList<ValueTuple<String,String>>

Containers to be initialized identified by it's database name and container name.

cosmosClientOptions
CosmosClientOptions

(Optional) client options

cancellationToken
CancellationToken

(Optional) Cancellation Token

Returns

A CosmosClient object.

Applies to

CreateAndInitializeAsync(String, String, IReadOnlyList<ValueTuple<String,String>>, CosmosClientOptions, CancellationToken)

Source:
CosmosClient.cs

Creates a new CosmosClient with the account endpoint URI string and TokenCredential. In addition to that it initializes the client with containers provided i.e The SDK warms up the caches and connections before the first call to the service is made. Use this to obtain lower latency while startup of your application. CosmosClient is thread-safe. Its recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. Please refer to the performance guide.

public static System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient> CreateAndInitializeAsync (string accountEndpoint, string authKeyOrResourceToken, System.Collections.Generic.IReadOnlyList<(string databaseId, string containerId)> containers, Microsoft.Azure.Cosmos.CosmosClientOptions cosmosClientOptions = default, System.Threading.CancellationToken cancellationToken = default);
static member CreateAndInitializeAsync : string * string * System.Collections.Generic.IReadOnlyList<ValueTuple<string, string>> * Microsoft.Azure.Cosmos.CosmosClientOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.CosmosClient>
Public Shared Function CreateAndInitializeAsync (accountEndpoint As String, authKeyOrResourceToken As String, containers As IReadOnlyList(Of ValueTuple(Of String, String)), Optional cosmosClientOptions As CosmosClientOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of CosmosClient)

Parameters

accountEndpoint
String

The cosmos service endpoint to use

authKeyOrResourceToken
String

The cosmos account key or resource token to use to create the client.

containers
IReadOnlyList<ValueTuple<String,String>>

Containers to be initialized identified by it's database name and container name.

cosmosClientOptions
CosmosClientOptions

(Optional) client options

cancellationToken
CancellationToken

(Optional) Cancellation Token

Returns

A CosmosClient object.

Examples

The CosmosClient is created with the AccountEndpoint, AccountKey or ResourceToken and 2 containers in the account are initialized

using Microsoft.Azure.Cosmos;
List<(string, string)> containersToInitialize = new List<(string, string)>
{ ("DatabaseName1", "ContainerName1"), ("DatabaseName2", "ContainerName2") };

CosmosClient cosmosClient = await CosmosClient.CreateAndInitializeAsync("account-endpoint-from-portal", 
                                                                        "account-key-from-portal",
                                                                        containersToInitialize)

// Dispose cosmosClient at application exit

Remarks

The returned reference doesn't guarantee credentials or connectivity validations because initialization doesn't make any network calls.

Applies to