CosmosClient CosmosClient CosmosClient Class

Definition

Provides a client-side logical representation of the Azure Cosmos DB account. This client can be used to configure and execute requests in the Azure Cosmos DB database service.

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 performance guide at .

public class CosmosClient : IDisposable
type CosmosClient = class
    interface IDisposable
Public Class CosmosClient
Implements IDisposable
Inheritance
CosmosClientCosmosClientCosmosClient
Implements

Examples

This example create a CosmosClient, Database, and a Container. The CosmosClient is created with the connection string and configured to use "East US 2" region.


using Microsoft.Azure.Cosmos;

CosmosClient cosmosClient = new CosmosClient(
            "connection-string-from-portal", 
            new CosmosClientOptions()
            {
                ApplicationRegion = Regions.EastUS2,
            });

Database db = await client.CreateDatabaseAsync("database-id");
Container container = await db.CreateContainerAsync("container-id");

// Dispose cosmosClient at application exit

This example create a CosmosClient, Database, and a Container. The CosmosClient is created with the AccountEndpoint, AccountKey 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,
            });

Database db = await client.CreateDatabaseAsync("database-id");
Container container = await db.CreateContainerAsync("container-id");

// Dispose cosmosClient at application exit

This example create a CosmosClient, Database, and a Container. The CosmosClient is created through builder pattern CosmosClientBuilder.


using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Fluent;

CosmosClient cosmosClient = new CosmosClientBuilder("connection-string-from-portal")
    .WithApplicationRegion("East US 2")
    .Build();

Database db = await client.CreateDatabaseAsync("database-id")
Container container = await db.CreateContainerAsync("container-id");

// Dispose cosmosClient at application exit

Remarks

Constructors

CosmosClient() CosmosClient() CosmosClient()

Create a new CosmosClient used for mock testing

CosmosClient(String, CosmosClientOptions) CosmosClient(String, CosmosClientOptions) CosmosClient(String, CosmosClientOptions)

Create 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 performance guide at .

CosmosClient(String, String, CosmosClientOptions) CosmosClient(String, String, CosmosClientOptions) CosmosClient(String, String, CosmosClientOptions)

Create 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 performance guide at .

Properties

ClientOptions ClientOptions ClientOptions

The CosmosClientOptions used initialize CosmosClient

Endpoint Endpoint Endpoint

Gets the endpoint Uri for the Azure Cosmos DB service.

Methods

CreateDatabaseAsync(String, Nullable<Int32>, RequestOptions, CancellationToken) CreateDatabaseAsync(String, Nullable<Int32>, RequestOptions, CancellationToken) CreateDatabaseAsync(String, Nullable<Int32>, RequestOptions, CancellationToken)

Send a request for creating a database.

A database manages users, permissions and a set of containers. Each Azure Cosmos DB Database Account is able to support multiple independent named databases, with the database being the logical container for data.

Each Database consists of one or more containers, each of which in turn contain one or more documents. Since databases are an administrative resource, the Service Master Key will be required in order to access and successfully complete any action using the User APIs.

CreateDatabaseIfNotExistsAsync(String, Nullable<Int32>, RequestOptions, CancellationToken) CreateDatabaseIfNotExistsAsync(String, Nullable<Int32>, RequestOptions, CancellationToken) CreateDatabaseIfNotExistsAsync(String, Nullable<Int32>, RequestOptions, CancellationToken)

Check if a database exists, and if it doesn't, create it. Only the database id is used to verify if there is an existing database. Other database properties such as throughput are not validated and can be different then the passed properties.

A database manages users, permissions and a set of containers. Each Azure Cosmos DB Database Account is able to support multiple independent named databases, with the database being the logical container for data.

Each Database consists of one or more containers, each of which in turn contain one or more documents. Since databases are an administrative resource, the Service Master Key will be required in order to access and successfully complete any action using the User APIs.

CreateDatabaseStreamAsync(DatabaseProperties, Nullable<Int32>, RequestOptions, CancellationToken) CreateDatabaseStreamAsync(DatabaseProperties, Nullable<Int32>, RequestOptions, CancellationToken) CreateDatabaseStreamAsync(DatabaseProperties, Nullable<Int32>, RequestOptions, CancellationToken)

Send a request for creating a database.

A database manages users, permissions and a set of containers. Each Azure Cosmos DB Database Account is able to support multiple independent named databases, with the database being the logical container for data.

Each Database consists of one or more containers, each of which in turn contain one or more documents. Since databases are an administrative resource, the Service Master Key will be required in order to access and successfully complete any action using the User APIs.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Dispose of cosmos client

Dispose() Dispose() Dispose()

Dispose of cosmos client

GetContainer(String, String) GetContainer(String, String) GetContainer(String, String)

Returns a proxy reference to a container.

GetDatabase(String) GetDatabase(String) GetDatabase(String)

Returns a proxy reference to a database.

GetDatabaseQueryIterator<T>(QueryDefinition, String, QueryRequestOptions) GetDatabaseQueryIterator<T>(QueryDefinition, String, QueryRequestOptions) GetDatabaseQueryIterator<T>(QueryDefinition, String, QueryRequestOptions)

This method creates a query for databases under an Cosmos DB Account using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition.

GetDatabaseQueryIterator<T>(String, String, QueryRequestOptions) GetDatabaseQueryIterator<T>(String, String, QueryRequestOptions) GetDatabaseQueryIterator<T>(String, String, QueryRequestOptions)

This method creates a query for databases under an Cosmos DB Account using a SQL statement. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

GetDatabaseQueryStreamIterator(String, String, QueryRequestOptions) GetDatabaseQueryStreamIterator(String, String, QueryRequestOptions) GetDatabaseQueryStreamIterator(String, String, QueryRequestOptions)

This method creates a query for databases under an Cosmos DB Account using a SQL statement. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition overload.

GetDatabaseQueryStreamIterator(QueryDefinition, String, QueryRequestOptions) GetDatabaseQueryStreamIterator(QueryDefinition, String, QueryRequestOptions) GetDatabaseQueryStreamIterator(QueryDefinition, String, QueryRequestOptions)

This method creates a query for databases under an Cosmos DB Account using a SQL statement with parameterized values. It returns a FeedIterator. For more information on preparing SQL statements with parameterized values, please see QueryDefinition.

ReadAccountAsync() ReadAccountAsync() ReadAccountAsync()

Read Azure Cosmos DB account properties AccountProperties

Applies to