CosmosClient Constructors
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
CosmosClient() |
Create a new CosmosClient used for mock testing |
CosmosClient(String, CosmosClientOptions) |
Creates a new CosmosClient with the connection string. 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. |
CosmosClient(String, AzureKeyCredential, CosmosClientOptions) |
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. 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. |
CosmosClient(String, TokenCredential, CosmosClientOptions) |
Creates a new CosmosClient with the account endpoint URI string and TokenCredential. 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. |
CosmosClient(String, String, CosmosClientOptions) |
Creates a new CosmosClient with the account endpoint URI string and account key. 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. |
CosmosClient()
- Source:
- CosmosClient.cs
Create a new CosmosClient used for mock testing
protected CosmosClient ();
Protected Sub New ()
Applies to
CosmosClient(String, CosmosClientOptions)
- Source:
- CosmosClient.cs
Creates a new CosmosClient with the connection string.
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 CosmosClient (string connectionString, Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions = default);
new Microsoft.Azure.Cosmos.CosmosClient : string * Microsoft.Azure.Cosmos.CosmosClientOptions -> Microsoft.Azure.Cosmos.CosmosClient
Public Sub New (connectionString As String, Optional clientOptions As CosmosClientOptions = Nothing)
Parameters
- connectionString
- String
The connection string to the cosmos account. ex: AccountEndpoint=https://XXXXX.documents.azure.com:443/;AccountKey=SuperSecretKey;
- clientOptions
- CosmosClientOptions
(Optional) client options
Examples
The CosmosClient is created with the connection string and configured to use "East US 2" region.
using Microsoft.Azure.Cosmos;
CosmosClient cosmosClient = new CosmosClient(
"account-endpoint-from-portal",
"account-key-from-portal",
new CosmosClientOptions()
{
ApplicationRegion = Regions.EastUS2,
});
// 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)
See also
Applies to
CosmosClient(String, AzureKeyCredential, CosmosClientOptions)
- 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.
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 CosmosClient (string accountEndpoint, Azure.AzureKeyCredential authKeyOrResourceTokenCredential, Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions = default);
new Microsoft.Azure.Cosmos.CosmosClient : string * Azure.AzureKeyCredential * Microsoft.Azure.Cosmos.CosmosClientOptions -> Microsoft.Azure.Cosmos.CosmosClient
Public Sub New (accountEndpoint As String, authKeyOrResourceTokenCredential As AzureKeyCredential, Optional clientOptions As CosmosClientOptions = Nothing)
Parameters
- accountEndpoint
- String
The cosmos service endpoint to use
- authKeyOrResourceTokenCredential
- AzureKeyCredential
AzureKeyCredential with master-key or resource token..
- clientOptions
- CosmosClientOptions
(Optional) client options
Examples
The CosmosClient is created with the AccountEndpoint, AccountKey or ResourceToken and configured to use "East US 2" region.
using Microsoft.Azure.Cosmos;
AzureKeyCredential keyCredential = new AzureKeyCredential("account-master-key/ResourceToken");
CosmosClient cosmosClient = new CosmosClient(
"account-endpoint-from-portal",
keyCredential,
new CosmosClientOptions()
{
ApplicationRegion = Regions.EastUS2,
});
....
// 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. The returned reference doesn't guarantee credentials or connectivity validations because creation doesn't make any network calls.
See also
Applies to
CosmosClient(String, TokenCredential, CosmosClientOptions)
- Source:
- CosmosClient.cs
Creates a new CosmosClient with the account endpoint URI string and TokenCredential.
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 CosmosClient (string accountEndpoint, Azure.Core.TokenCredential tokenCredential, Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions = default);
new Microsoft.Azure.Cosmos.CosmosClient : string * Azure.Core.TokenCredential * Microsoft.Azure.Cosmos.CosmosClientOptions -> Microsoft.Azure.Cosmos.CosmosClient
Public Sub New (accountEndpoint As String, tokenCredential As TokenCredential, Optional clientOptions As CosmosClientOptions = Nothing)
Parameters
- accountEndpoint
- String
The cosmos service endpoint to use.
- tokenCredential
- TokenCredential
TokenCredentialThe token to provide AAD token for authorization.
- clientOptions
- CosmosClientOptions
(Optional) client options
Remarks
The returned reference doesn't guarantee credentials or connectivity validations because creation doesn't make any network calls.
Applies to
CosmosClient(String, String, CosmosClientOptions)
- Source:
- CosmosClient.cs
Creates a new CosmosClient with the account endpoint URI string and account key.
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 CosmosClient (string accountEndpoint, string authKeyOrResourceToken, Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions = default);
new Microsoft.Azure.Cosmos.CosmosClient : string * string * Microsoft.Azure.Cosmos.CosmosClientOptions -> Microsoft.Azure.Cosmos.CosmosClient
Public Sub New (accountEndpoint As String, authKeyOrResourceToken As String, Optional clientOptions As CosmosClientOptions = Nothing)
Parameters
- accountEndpoint
- String
The cosmos service endpoint to use
- authKeyOrResourceToken
- String
The cosmos account key or resource token to use to create the client.
- clientOptions
- CosmosClientOptions
(Optional) client options
Examples
The CosmosClient is created with the AccountEndpoint, AccountKey or ResourceToken and configured to use "East US 2" region.
using Microsoft.Azure.Cosmos;
CosmosClient cosmosClient = new CosmosClient(
"account-endpoint-from-portal",
"account-key-from-portal",
new CosmosClientOptions()
{
ApplicationRegion = Regions.EastUS2,
});
// Dispose cosmosClient at application exit
Remarks
The returned reference doesn't guarantee credentials or connectivity validations because creation doesn't make any network calls.