Soru

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

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?

  1. 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 }
    );
    Cevap
  2. B
    ItemResponse<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)
    );
  3. C
    ItemResponse<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 }
    );
  4. D
    ItemResponse<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 }
    );

Cevap

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.

Adım Adım Çözüm

1
Call ReplaceItemAsync using the SDK v3 client.
The item is successfully replaced, returning an ItemResponse object.
SDK v3 requires passing the item, the item ID, and the PartitionKey for point-replace operations.
2
Extract the session token from the write response headers.
The session token string is captured via writeResponse.Headers.Session.
This token is needed to maintain consistency state across separate client instances.
3
Pass the session token in the ItemRequestOptions during the read operation.
The second client instance reads the updated data immediately.
Without passing the session token, Session consistency guarantees are limited to the client instance that performed the write.

Anahtar Kavram

Handling item operations and managing cross-client session consistency using the Azure Cosmos DB .NET SDK v3.
Bu soruyu puanla