A SaaS application uses the Azure Cosmos DB .NET SDK v3 to store user settings in a container. The container is configured with Session consistency and is partitioned by `TenantId`. You are writing C# code to replace a user's settings document.
To ensure data consistency and verify the update immediately from a separate client instance, you need to execute the write and pass the session state to the second client.
Which code segment should you use to achieve this?
- ItemResponse<UserSettings> writeResponse = await container.ReplaceItemAsync<UserSettings>(
settings,
settings.Id,
new PartitionKey(settings.TenantId)
);
string sessionToken = writeResponse.Headers.Session;
ItemResponse<UserSettings> readResponse = await otherContainer.ReadItemAsync<UserSettings>(
settings.Id,
new PartitionKey(settings.TenantId),
new ItemRequestOptions { SessionToken = sessionToken }
);Answer - BItemResponse<UserSettings> writeResponse = await container.ReplaceItemAsync<UserSettings>(
settings,
settings.Id,
new PartitionKey(settings.TenantId)
);
ItemResponse<UserSettings> readResponse = await otherContainer.ReadItemAsync<UserSettings>(
settings.Id,
new PartitionKey(settings.TenantId)
); - CItemResponse<UserSettings> writeResponse = await container.ReplaceItemAsync<UserSettings>(
settings,
settings.Id,
new PartitionKey(settings.IsActive.ToString())
);
string sessionToken = writeResponse.Headers.Session;
ItemResponse<UserSettings> readResponse = await otherContainer.ReadItemAsync<UserSettings>(
settings.Id,
new PartitionKey(settings.IsActive.ToString()),
new ItemRequestOptions { SessionToken = sessionToken }
); - DItemResponse<UserSettings> writeResponse = await container.ReplaceItemAsync<UserSettings>(
settings,
settings.Id
);
string sessionToken = writeResponse.Headers.Session;
ItemResponse<UserSettings> readResponse = await otherContainer.ReadItemAsync<UserSettings>(
settings.Id,
new ItemRequestOptions { SessionToken = sessionToken }
);
Answer
The correct option is the code segment that replaces the item using the TenantId partition key, extracts the session token from the write response headers, and applies it to the read request options of the second client.
The correct code segment uses the Azure Cosmos DB .NET SDK v3 ReplaceItemAsync method with the correct parameters (item, id, PartitionKey). By default, Session consistency is scoped to a single client instance. To guarantee read-your-writes across multiple client instances, you must extract the session token from the write response header (Headers.Session) and pass it to the read operation of the second client using ItemRequestOptions.
Step-by-Step Solution
Key Concept
Handling item operations and managing cross-client session consistency using the Azure Cosmos DB .NET SDK v3.