DigitalTwinsClient.CreateOrReplaceDigitalTwinAsync<T> Method

Definition

Creates a digital twin asynchronously. If the provided digital twin Id is already in use, then this will attempt to replace the existing digital twin with the provided digital twin..

public virtual System.Threading.Tasks.Task<Azure.Response<T>> CreateOrReplaceDigitalTwinAsync<T> (string digitalTwinId, T digitalTwin, Azure.ETag? ifNoneMatch = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateOrReplaceDigitalTwinAsync : string * 'T * Nullable<Azure.ETag> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<'T>>
override this.CreateOrReplaceDigitalTwinAsync : string * 'T * Nullable<Azure.ETag> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<'T>>
Public Overridable Function CreateOrReplaceDigitalTwinAsync(Of T) (digitalTwinId As String, digitalTwin As T, Optional ifNoneMatch As Nullable(Of ETag) = Nothing, 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.

digitalTwin
T

The application/json digital twin to create.

ifNoneMatch
Nullable<ETag>

If-None-Match header that makes the request method conditional on a recipient cache or origin server either not having any current representation of the target resource. For more information about this property, see RFC 7232. Acceptable values are null or "". If ifNonMatch option is null the service will replace the existing entity with the new entity. If ifNoneMatch option is "" (or All) the service will reject the request if the entity already exists. An optional ETag to only make the request if the value does not match on the service.

cancellationToken
CancellationToken

The cancellation token.

Returns

The created 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 or digitalTwin is null.

Examples

var customTwin = new CustomDigitalTwin
{
    Id = customDtId,
    Metadata = { ModelId = modelId },
    Prop1 = "Prop1 val",
    Prop2 = 987,
    Component1 = new MyCustomComponent
    {
        ComponentProp1 = "Component prop1 val",
        ComponentProp2 = 123,
    },
};
Response<CustomDigitalTwin> createCustomDigitalTwinResponse = await client.CreateOrReplaceDigitalTwinAsync(customDtId, customTwin);
Console.WriteLine($"Created digital twin '{createCustomDigitalTwinResponse.Value.Id}'.");

Remarks

For more samples, see our repo samples.

Applies to