Steps to reproduce:

  1. Try to update any key using ModuleClient.UpdateReportedPropertiesAsync method.
  2. Get the latest twin using ModuleClient.GetTwinAsync method.
  3. When you get the twin after updating then it contains the older version of reported properties.

Note: The code is working in the Local environment but when we deploy it in azure as a custom module then it stops working.

I have attached a log for reference.

Twin Before Update:
{
  "deviceId": null,
  "etag": null,
  "version": null,
  "properties": {
    "desired": {
      "general": {
        "autoUpdate": false
      },
      "specific": {
        "autoUpdate": false
      },
      "$version": 1
    },
    "reported": {
      "$version": 1
    }
  }
}

Twin After Update:
{
  "deviceId": null,
  "etag": null,
  "version": null,
  "properties": {
    "desired": {
      "general": {
        "autoUpdate": false
      },
      "specific": {
        "autoUpdate": false
      },
      "$version": 1
    },
    "reported": {
      "$version": 1
    }
  }
}

Below code I have tried

public async Task UpdateReportedProperties(TwinCollection twinCollection) 
{ 

await _moduleClient.UpdateReportedPropertiesAsync(twinCollection); 

ModuleTwin = await _moduleClient!.GetTwinAsync(); 

}
1

There are 1 best solutions below

2
Sampath On

The sample code below interacts with Azure IoT Hub using the Azure IoT SDK. It retrieves the device twin, updates the reported properties, and then retrieves the updated twin using UpdateReportedPropertiesAsync and GetTwinAsync. The code utilizes Microsoft.Azure.Devices.Client, GetTwinAsync, and UpdateReported (or) with REST API with C#.



   moduleClient = ModuleClient.CreateFromConnectionString(connectionString);

   await moduleClient.OpenAsync();

   // Get the twin before update
   Twin twinBefore = await moduleClient.GetTwinAsync();
   Console.WriteLine("Twin Before Update:");
   Console.WriteLine(JsonConvert.SerializeObject(twinBefore, Formatting.Indented));

   // Update reported properties
   var patch = new
   {
       properties = new
       {
           reported = new
           {
               info = new
               {
                   origin = "Argentina",
                   version = "14.01"
               }
           }
       }
   };

   var patchJson = JsonConvert.SerializeObject(patch);
   var patchBytes = Encoding.UTF8.GetBytes(patchJson);
   var patchTwin = new TwinCollection(patchJson);

   await moduleClient.UpdateReportedPropertiesAsync(patchTwin);

   // Get the twin after update
   Twin twinAfter = await moduleClient.GetTwinAsync();
   Console.WriteLine("\nTwin After Update:");
   Console.WriteLine(JsonConvert.SerializeObject(twinAfter, Formatting.Indented));

   await moduleClient.CloseAsync();

Output:

enter image description here

enter image description here

Azure: enter image description here

The code below is a sample for connecting to Azure IoT Devices Get Twin using the REST API.

string deviceid = "<deviceid>";
        string hubName = "<your_iot_hub_name>";
        string APIVersion = "2020-05-31-preview";
        string URL = $"https://{hubName}.azure-devices.net/twins/{deviceid}?api-version={APIVersion}";
        string SAS_TOKEN = "<IOT_SAS_TOKEN>";
 string resultStr = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.Headers.Add("Authorization", SAS_TOKEN);
            request.ContentType = "application/json";

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                resultStr = reader.ReadToEnd();
            }

            Console.WriteLine("Device Twin Information:");
            Console.WriteLine(resultStr);


Azure IoT Devices Update Twin can be done using the REST API with this link.