What's the etag in IoT Hub device twins for?

Viewed 988

I'm developing a web application in .NET Core 3 and I'm using the Microsoft.Azure.Devices.RegistryManager to access an Azure Iot hub.

When I call registryManager.UpdateTwinAsync(), I have to send in the "etag". And sometimes the etag changes and I get errors stating that there is an etag mismatch.

I basically don't understand what the etag's purpose is and how I can update device twins in a safe manner. Anyone?

I tried catching those exceptions and fetching the etag from the device twin and retrying up to 15 times in a row with 2 seconds wait in between and that helps, but this error pops up every now and then. I would think that applying the device id would be enough to update a device twin but apparently not

Please help!

3 Answers

So in a simple terms we can say that ETag - Entity tag will keep changing when ever we see there is a change in device status or any other updates.

A string representing a weak ETag for the device identity, as per RFC7232 Section 2.3 Etag.

An entity-tag is an opaque validator for differentiating between multiple representations of the same resource, regardless of whether those multiple representations are due to resource state changes over time

For example: Enable and Disable an IoT Device and check the 'Get device Info' from Visual Studio Code (Azure IoT Hub Extension)

Step 1) I have an initial IoT device with status Enabled and I can see an Etag value.

Step 2) When I disabled the device on IoT Hub portal, and then did second query 'Get Device Info' call, I see a different value on ETag.

enter image description here

Step 3) Now when I queried for the second time (like after 5 mins interval), I see the Etag value remains same for the same disabled state of device.

enter image description here

**For more info please visit Device identity properties, Module identity properties

**Please let us know if you need more help on this!

My understanding is that the ETag is used to ensure consistency on the device twin when you're updating it. It's read-only so you would get it from your device twin and pass it back to UpdateTwinAsync() with your changes. Link

I'm not sure what you mean when you say the ETag is changing - can you provide some code of what you are doing (or clarify when the ETag is changing)?

I created a video on YouTube that demos how to read/write Azure Device Twins, so feel free to check it out, hopefully it helps you! Link

Related