References :
https://azure.microsoft.com/en-us/blog/iot-hub-message-routing-now-with-routing-on-message-body/
How to send a json object instead of a string with Azure Client SDK
I can send IOT messages successfully and store these messages in Azure storage. On reviewing the messages, the body is BASE64 encoded. I want the JSON within the body in the message to be shown as a string so I can process the contents in ADF.
I have added to my message the properties as below, as suggested by the reference posts above:
mes.ContentEncoding = "utf-8";
mes.ContentType = "application/json";
On doing this, I get the message below when I try and send the message. The app compiles ok, it's only when my app sends the message.
Method not found: 'Void Microsoft.Azure.Devices.Client.Message.set_ContentEncoding(System.String)'.
Comment the lines out, and it works fine again.
What am I doing wrong?
private async void SendDeviceToCloudMessagesAsyncDcbIot(DcbIotPayload dataPayload)
{
if (string.IsNullOrWhiteSpace(dataPayload.did))
{
DpmIotLog.LogToFile("no deviceId in data payload", DpmIotLog.LogCondition.High);
return;
}
dataPayload.t = DateTime.Now.ToString();
var messageString = JsonConvert.SerializeObject(dataPayload);
var messageSize = Encoding.ASCII.GetByteCount(messageString);
DpmIotLog.LogToFile($"Message Len {messageSize}", DpmIotLog.LogCondition.Verbose);
DpmIotLog.LogToFile($"Message {messageString}", DpmIotLog.LogCondition.Verbose);
byte[] messageBytes = Encoding.UTF8.GetBytes(messageString);
using (var mes = new Message(messageBytes))
{
// Set message body type and content encoding.
mes.ContentEncoding = "utf-8";
mes.ContentType = "application/json";
try
{
if (messageSize > 7000)
{
var ex = new Exception("PayloadSizeException");
DpmIotLog.LogToFile("payload over 7000 bytes", ex, DpmIotLog.LogCondition.High);
throw ex;
}
DpmIotLog.LogToFile($"Submitting Message to HUB", DpmIotLog.LogCondition.Verbose);
Task t = deviceClient.SendEventAsync(mes);
if (await Task.WhenAny(t, Task.Delay(10000)) == t)
{
MessageSent?.Invoke(true);
DpmIotLog.LogToFile($"Message Sent", DpmIotLog.LogCondition.Verbose);
deviceClient.Dispose();
}
else
{
MessageSent?.Invoke(false);
DpmIotLog.LogToFile($"Message Fail", DpmIotLog.LogCondition.Verbose);
deviceClient.Dispose();
}
}
catch (DeviceNotFoundException)
{
DpmIotLog.LogToFile($"invalid or disabled device ID : {dataPayload.did}", DpmIotLog.LogCondition.Critical);
}
catch (DeviceDisabledException)
{
DpmIotLog.LogToFile($"IOT disabled device ID : {dataPayload.did}", DpmIotLog.LogCondition.Critical);
}
catch (Exception ex)
{
DpmIotLog.LogToFile("task to upload payload to server failure", ex, DpmIotLog.LogCondition.Critical);
}
}
}