Skip to content

Commit

Permalink
Let user get payloads as bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
David R. Williamson committed Mar 28, 2023
1 parent 1c5cd47 commit b8604d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion iothub/device/src/DirectMethod/DirectMethodRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ public bool TryGetPayload<T>(out T payload)
payload = PayloadConvention.GetObject<T>(Payload);
return true;
}
catch (Exception)
catch (Exception ex)
{
// In case the value cannot be converted using the serializer,
// then return false with the default value of the type <T> passed in.
if (Logging.IsEnabled)
Logging.Error(this, ex, nameof(TryGetPayload));
}

return false;
Expand All @@ -144,5 +146,14 @@ public string GetPayloadAsJsonString()
? null
: DefaultPayloadConvention.Encoding.GetString(Payload);
}

/// <summary>
/// Get the raw payload bytes.
/// </summary>
/// <returns>A copy of the raw payload as a byte array.</returns>
public byte[] GetPayloadAsBytes()
{
return (byte[])Payload.Clone();
}
}
}
9 changes: 9 additions & 0 deletions iothub/device/src/Messaging/IncomingMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ public bool TryGetPayload<T>(out T payload)
return false;
}

/// <summary>
/// Get the raw payload bytes.
/// </summary>
/// <returns>A copy of the raw payload as a byte array.</returns>
public byte[] GetPayloadAsBytes()
{
return (byte[])_payload.Clone();
}

private T GetSystemProperty<T>(string key)
{
return SystemProperties.TryGetValue(key, out object value) ? (T)value : default;
Expand Down

0 comments on commit b8604d7

Please sign in to comment.