Soru

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

You are developing a C# gaming service that stores player session state in Azure Cosmos DB using the .NET SDK v3. Initially, the container was partitioned by a low-cardinality property path `/sessionType` (which had a static value of 'ActiveSession' for all items), causing hot partitions and high latency. To resolve this, you re-create the container with the partition key path set to `/playerId`. You need to write a method to upsert a player's session. Which C# code segment should you use?

  1. A
    PlayerSession session = new PlayerSession { Id = "session-901", PlayerId = "p-888", SessionType = "ActiveSession" };
    ItemResponse<PlayerSession> response = await container.UpsertItemAsync<PlayerSession>(
    session,
    new PartitionKey(session.SessionType)
    );
  2. PlayerSession session = new PlayerSession { Id = "session-901", PlayerId = "p-888", SessionType = "ActiveSession" };
    ItemResponse<PlayerSession> response = await container.UpsertItemAsync<PlayerSession>(
    session,
    new PartitionKey(session.PlayerId)
    );
    Cevap
  3. C
    PlayerSession session = new PlayerSession { Id = "session-901", PlayerId = "p-888", SessionType = "ActiveSession" };
    ItemResponse<PlayerSession> response = await container.UpsertItemAsync<PlayerSession>(
    session
    );
  4. D
    PlayerSession session = new PlayerSession { Id = "session-901", PlayerId = "p-888", SessionType = "ActiveSession" };
    ResourceResponse<Document> response = await client.UpsertDocumentAsync(
    UriFactory.CreateDocumentCollectionUri("GameDb", "Sessions"),
    session,
    new RequestOptions { PartitionKey = new PartitionKey(session.PlayerId) }
    );

Cevap

The option that invokes UpsertItemAsync using the session object and a PartitionKey constructed with session.PlayerId
The correct answer correctly calls container.UpsertItemAsync with the session object and a new PartitionKey instance set to session.PlayerId. This is compatible with the container's partition key path of /playerId, ensuring the item is routed to the correct partition, and uses the correct .NET SDK v3 types.

Adım Adım Çözüm

1
Identify the target SDK version requirement.
The target SDK is the .NET SDK v3, which utilizes CosmosClient, Database, Container, and ItemResponse.
This filters out obsolete SDK v2 classes such as DocumentClient, Document, and ResourceResponse.
2
Analyze the container partition key configuration.
The partition key path is set to /playerId. Therefore, the partition key value passed to the operation must be the PlayerId of the document.
Using any other property (like SessionType) will cause a partition key mismatch runtime error and can lead to hot partitions.
3
Verify SDK best practices for item operations.
SDK v3 requires explicitly passing the PartitionKey as a parameter in methods like UpsertItemAsync.
Omitting the PartitionKey causes additional processing latency or runtime exceptions during serialization/deserialization.

Anahtar Kavram

Performing item upsert operations using the Cosmos DB .NET SDK v3 with an explicit partition key value that aligns with the container partition key path.
Tahmini Süre:1m 30s
Bu soruyu puanla