Question

Difficulty: Very hardPerform Container and Item Operations in Azure Cosmos DB using SDK

You are developing a .NET microservice that processes user orders in a multi-tenant SaaS application. Each tenant has their own partition in an Azure Cosmos DB SQL API container, and all operations within a single order lifecycle must be executed within a single transaction. You need to write C# code using the Azure Cosmos DB .NET SDK v3 to configure the client, access the container, and run a transactional batch of operations. In which sequence should you perform the steps to configure the client and execute the transactional batch?

  1. 1Instantiate the CosmosClient using a connection string and custom CosmosClientOptions to configure connection properties.
  2. 2Retrieve a Container reference by calling GetDatabase and then GetContainer from the CosmosClient instance.
  3. 3Create a PartitionKey object passing the value of the partition key for the specific tenant.
  4. 4Call CreateTransactionalBatch on the Container reference, passing the PartitionKey object.
  5. 5Chain operations like CreateItem or ReplaceItem to the TransactionalBatch instance, and then call ExecuteAsync to execute the transaction.

Answer

To perform a transactional batch operation in Azure Cosmos DB using the .NET SDK v3, you must first initialize a singleton CosmosClient with the required options. Next, obtain the Database and Container references. Since transactional batches operate on a single logical partition, you must define the PartitionKey. Then, initialize the TransactionalBatch using CreateTransactionalBatch on the container with the partition key. Finally, chain the operations onto the batch and call ExecuteAsync.
The correct sequence starts with client initialization (CosmosClient), followed by drilling down to the specific database and container references. Because transactional batching in Azure Cosmos DB requires operations to share the same partition key, the PartitionKey instance must be defined prior to calling CreateTransactionalBatch on the container. Finally, individual operations are chained to the batch, and ExecuteAsync is called to execute the entire transaction.

Step-by-Step Solution

1
Initialize CosmosClient
An active connection client to the Azure Cosmos DB account.
CosmosClient is the top-level SDK client needed for all subsequent operations.
2
Obtain Container Reference
A Container object representing the target collection.
Container operations and transaction builders are scoped to the container level.
3
Define PartitionKey
A PartitionKey structure representing the tenant ID.
Azure Cosmos DB requires the logical partition key value to scope a transactional batch.
4
Build TransactionalBatch
A TransactionalBatch object initialized for the target partition key.
The CreateTransactionalBatch factory method on the container initiates the transactional workflow.
5
Chain Operations and Execute
A TransactionalBatchResponse containing the status and results of each chained operation.
Chained operations are added to the batch, and ExecuteAsync transmits them as a single atomic transaction.

Key Concept

Initiating CosmosClient, obtaining Container references, constructing a PartitionKey, and executing a TransactionalBatch with .NET SDK v3.
Rate this question