Soru

Zorluk: OrtaPerform Container and Item Operations in Azure Cosmos DB using SDK

You are developing a C# service that manages smart-home IoT devices using the Azure Cosmos DB .NET SDK v3. The container stores configuration settings and is partitioned by the `/deviceId` path. The database uses Session consistency. When a device's settings are updated, your service must replace the existing document, retrieve the resulting session token to pass to other reading clients, and ensure the write operation is routed to the correct partition. Which C# code segment should you use?

  1. ItemResponse<DeviceConfig> response = await container.ReplaceItemAsync<DeviceConfig>(config, config.Id, new PartitionKey(config.DeviceId));
    string sessionToken = response.Headers.Session;
    Cevap
  2. B
    ItemResponse<DeviceConfig> response = await container.ReplaceItemAsync<DeviceConfig>(config, config.Id, new PartitionKey(config.Id));
    string sessionToken = response.Headers.Session;
  3. C
    ItemResponse<DeviceConfig> response = await container.ReplaceItemAsync<DeviceConfig>(config, config.Id, new PartitionKey("all-devices"));
    string sessionToken = response.Headers.Session;
  4. D
    ItemResponse<DeviceConfig> response = await container.ReplaceItemAsync<DeviceConfig>(config, config.Id, new PartitionKey(config.DeviceId));
    string sessionToken = response.RequestCharge.ToString();

Cevap

The correct option is the one that calls ReplaceItemAsync using config.Id and a PartitionKey instantiated with config.DeviceId, and then retrieves the session token using response.Headers.Session.
The correct code segment uses the ReplaceItemAsync method of the Container class to update the item. It correctly passes the unique document ID and a PartitionKey initialized with the deviceId. The session token is then successfully extracted from the Headers.Session property of the ItemResponse object.

Adım Adım Çözüm

1
Select the correct Cosmos DB .NET SDK v3 item replacement method.
Use container.ReplaceItemAsync<T>, passing the item, its ID, and the partition key value.
This performs a point-update (replace) on the existing item.
2
Ensure the write operation is correctly partitioned.
Provide the deviceId value to the PartitionKey constructor (new PartitionKey(config.DeviceId)).
The container partition key path is /deviceId, so all operations on a device's settings must target its deviceId partition.
3
Retrieve the session token for consistency propagation.
Access response.Headers.Session.
The session token is returned in the HTTP headers of the SDK response.

Anahtar Kavram

Replacing items in Azure Cosmos DB using the .NET SDK v3 and capturing the Session consistency token.
Tahmini Süre:1m 30s
Bu soruyu puanla