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?
- APlayerSession session = new PlayerSession { Id = "session-901", PlayerId = "p-888", SessionType = "ActiveSession" };
ItemResponse<PlayerSession> response = await container.UpsertItemAsync<PlayerSession>(
session,
new PartitionKey(session.SessionType)
); - 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 - CPlayerSession session = new PlayerSession { Id = "session-901", PlayerId = "p-888", SessionType = "ActiveSession" };
ItemResponse<PlayerSession> response = await container.UpsertItemAsync<PlayerSession>(
session
); - DPlayerSession 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
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