Super late reply here, but I was having the same issue. The thing is the, string in your body section of the json is encoded in a base64 string. You first need to decode it, and then you'll have access to all the data you have passed in. In your example code above, I see that you passed:
{
"messageId": 451,
"deviceID": "Raspberry Pi Web Client",
"temperature": 25.372045922278556,
"humidity": 71.67035737305419
}
So what I do is first deserialize the first incoming message, then access the body field which is the base64 string, and decode and deserialize it, and now you'd have access to your data fields:
try
{
var cred = new DefaultAzureCredential();
var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), cred);
log.LogInformation($"ADT service client connection created");
if (eventGridEvent != null && eventGridEvent.Data != null)
{
log.LogInformation(eventGridEvent.Data.ToString());
var deviceMessage = (JObject)JsonConvert.DeserializeObject(eventGridEvent.Data.ToString());
var decodedBytes = Convert.FromBase64String(deviceMessage["body"].ToString());
var jsonString = Encoding.UTF8.GetString(decodedBytes);
log.LogInformation(jsonString);
var dataMessage = (JObject)JsonConvert.DeserializeObject(jsonString);
// get our device id, temp and humidity from the object
string deviceId = (string)deviceMessage["systemProperties"]["iothub-connection-device-id"];
var messageId= dataMessage["messageId"];
var deviceID= dataMessage["deviceID"];
var temperature= dataMessage["temperature"];
var humidity= dataMessage["humidity"];
var updateTwinData = new JsonPatchDocument();
updateTwinData.AppendReplace("/messageId", messageId.Value<double>());
updateTwinData.AppendReplace("/deviceID", deviceID.Value<double>());
updateTwinData.AppendReplace("/temperature", temperature.Value<double>());
updateTwinData.AppendReplace("/humidity", humidity.Value<double>());
await client.UpdateDigitalTwinAsync(deviceId, updateTwinData);
}
}
Hope this helps.