Delen via


DigitalTwinsClient.PublishComponentTelemetryAsync Method

Definition

Publishes telemetry from a digital twin's component asynchronously. The result is then consumed by one or many destination endpoints (subscribers) defined under DigitalTwinsEventRoute. These event routes need to be set before publishing a telemetry message, in order for the telemetry message to be consumed.

public virtual System.Threading.Tasks.Task<Azure.Response> PublishComponentTelemetryAsync (string digitalTwinId, string componentName, string messageId, string payload, DateTimeOffset? timestamp = default, System.Threading.CancellationToken cancellationToken = default);
abstract member PublishComponentTelemetryAsync : string * string * string * string * Nullable<DateTimeOffset> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response>
override this.PublishComponentTelemetryAsync : string * string * string * string * Nullable<DateTimeOffset> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response>
Public Overridable Function PublishComponentTelemetryAsync (digitalTwinId As String, componentName As String, messageId As String, payload As String, Optional timestamp As Nullable(Of DateTimeOffset) = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response)

Parameters

digitalTwinId
String

The Id of the digital twin.

componentName
String

The name of the DTDL component.

messageId
String

A unique message identifier (within the scope of the digital twin id) that is commonly used for de-duplicating messages. Defaults to a random GUID if argument is null.

payload
String

The application/json telemetry payload to be sent.

timestamp
Nullable<DateTimeOffset>

An RFC 3339 timestamp that identifies the time the telemetry was measured. It defaults to the current date/time UTC.

cancellationToken
CancellationToken

The cancellation token.

Returns

The HTTP response Response.

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 componentName or payload is null.

Examples

// construct your json telemetry payload by serializing a dictionary.
var telemetryPayload = new Dictionary<string, int>
{
    { "ComponentTelemetry1", 9 }
};
await client.PublishComponentTelemetryAsync(
    twinId,
    "Component1",
    Guid.NewGuid().ToString(),
    JsonSerializer.Serialize(telemetryPayload));
Console.WriteLine($"Published component telemetry message to twin '{twinId}'.");

Remarks

For more samples, see our repo samples.

Applies to

See also