You are developing a C# backend service that manages user profiles in a multi-tenant SaaS application. User profile items are stored in an Azure Cosmos DB container with the partition key path `/tenantId`. The Azure Cosmos DB account is configured to use Session consistency.
A user updates their profile through one instance of the service, which returns a session token. A separate service instance must immediately read this updated profile. To guarantee read-your-writes consistency across the separate service instances with the lowest latency and RU cost, you must perform a point read using the Cosmos DB .NET SDK v3.
Which C# code snippet should you use?
- csharp
ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(
id: "user-99",
partitionKey: new PartitionKey("tenant-abc"),
requestOptions: options
);
Cevap - Bcsharp
ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(
id: "user-99",
partitionKey: new PartitionKey("tenant-abc")
); - Ccsharp
ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(
id: "user-99",
partitionKey: new PartitionKey("active"),
requestOptions: options
); - Dcsharp
FeedIterator<UserProfile> iterator = container.GetItemQueryIterator<UserProfile>(
"SELECT * FROM c WHERE c.id = 'user-99'"
);
Cevap
The correct code snippet performs a point read using ReadItemAsync, passes the tenant ID as a PartitionKey object, and includes the session token within ItemRequestOptions.
The correct snippet uses ReadItemAsync to perform a point read (the lowest latency and cost operation for single item retrieval). By passing the partition key as new PartitionKey("tenant-abc") and setting the SessionToken property in ItemRequestOptions, it successfully guarantees read-your-writes consistency across separate service instances.
Adım Adım Çözüm
Anahtar Kavram
Performing point reads with session tokens using the Azure Cosmos DB .NET SDK v3 to guarantee read-your-writes consistency across clients.
Tahmini Süre:1m 30s