Container.ReadItemAsync<T>(String, PartitionKey, ItemRequestOptions, CancellationToken) Method

Definition

Reads a item from the Azure Cosmos service as an asynchronous operation.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ItemResponse<T>> ReadItemAsync<T> (string id, Microsoft.Azure.Cosmos.PartitionKey partitionKey, Microsoft.Azure.Cosmos.ItemRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadItemAsync : string * Microsoft.Azure.Cosmos.PartitionKey * Microsoft.Azure.Cosmos.ItemRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ItemResponse<'T>>
Public MustOverride Function ReadItemAsync(Of T) (id As String, partitionKey As PartitionKey, Optional requestOptions As ItemRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ItemResponse(Of T))

Type Parameters

T

Parameters

id
String

The Cosmos item id

partitionKey
PartitionKey

The partition key for the item.

requestOptions
ItemRequestOptions

(Optional) The options for the item request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

Task<ItemResponse<T>>

A Task containing a ItemResponse<T> which wraps the read resource record.

Exceptions

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
429TooManyRequests - This means you have exceeded the number of request units per second.

Examples

public class ToDoActivity{
    public string id {get; set;}
    public string status {get; set;}
}

ToDoActivity toDoActivity = await this.container.ReadItemAsync<ToDoActivity>("id", new PartitionKey("partitionKey"));

Remarks

Items contain meta data that can be obtained by mapping these meta data attributes to properties in T.

  • "_ts": Gets the last modified time stamp associated with the item from the Azure Cosmos DB service.
  • "_etag": Gets the entity tag associated with the item from the Azure Cosmos DB service.
  • "ttl": Gets the time to live in seconds of the item in the Azure Cosmos DB service.

Applies to