DigitalTwinsClient.GetDigitalTwinAsync<T> Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a digital twin asynchronously.
public virtual System.Threading.Tasks.Task<Azure.Response<T>> GetDigitalTwinAsync<T> (string digitalTwinId, System.Threading.CancellationToken cancellationToken = default);
abstract member GetDigitalTwinAsync : string * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<'T>>
override this.GetDigitalTwinAsync : string * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<'T>>
Public Overridable Function GetDigitalTwinAsync(Of T) (digitalTwinId As String, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response(Of T))
Type Parameters
- T
The type to deserialize the digital twin to.
Parameters
- digitalTwinId
- String
The Id of the digital twin.
- cancellationToken
- CancellationToken
The cancellation token.
Returns
The deserialized application/json digital twin and the HTTP response Response<T>.
Exceptions
The exception that captures the errors from the service. Check the ErrorCode and Status properties for more details.
The exception is thrown when digitalTwinId
is null
.
Examples
This sample demonstrates getting and deserializing a digital twin into a custom data type.
Response<CustomDigitalTwin> getCustomDtResponse = await client.GetDigitalTwinAsync<CustomDigitalTwin>(customDtId);
CustomDigitalTwin customDt = getCustomDtResponse.Value;
Console.WriteLine($"Retrieved and deserialized digital twin {customDt.Id}:\n\t" +
$"ETag: {customDt.ETag}\n\t" +
$"ModelId: {customDt.Metadata.ModelId}\n\t" +
$"Prop1: [{customDt.Prop1}] last updated on {customDt.Metadata.Prop1.LastUpdatedOn}\n\t" +
$"Prop2: [{customDt.Prop2}] last updated on {customDt.Metadata.Prop2.LastUpdatedOn}\n\t" +
$"ComponentProp1: [{customDt.Component1.ComponentProp1}] last updated {customDt.Component1.Metadata.ComponentProp1.LastUpdatedOn}\n\t" +
$"ComponentProp2: [{customDt.Component1.ComponentProp2}] last updated {customDt.Component1.Metadata.ComponentProp2.LastUpdatedOn}");
Remarks
A strongly typed object type such as BasicDigitalTwin can be used as a generic type for T
to indicate what type is used to deserialize the response value. It may also be deserialized into custom digital twin types that extend the BasicDigitalTwin with additional strongly typed properties provided that you know the definition of the retrieved digital twin prior to deserialization.
For more samples, see our repo samples.