public static QueueMessage ToQueueMessage(CloudQueueMessage message) { if (message == null) { return(null); } byte[] mb = message.AsBytes; QueueMessage result; if (!IsCustomMessage(mb)) { result = new QueueMessage(CreateId(message), mb); } else { using (var ms = new MemoryStream(mb)) { //skip forward custom message flag ms.Seek(CustomFlagBytes.Length, SeekOrigin.Begin); //read the custom properties length int cpl; using (var br = new BinaryReader(ms, Encoding.UTF8, true)) { cpl = br.ReadInt32(); } //read the actual properties byte[] propBytes = new byte[cpl]; ms.Read(propBytes, 0, cpl); string propString = Encoding.UTF8.GetString(propBytes); JsonProps props = propString.AsJsonObject <JsonProps>(); //read message data byte[] leftovers = ms.ToByteArray(); result = new QueueMessage(CreateId(message), leftovers); foreach (JsonProp prop in props.Properties) { result.Properties[prop.Name] = prop.Value; } } } result.DequeueCount = message.DequeueCount; return(result); }
public static CloudQueueMessage ToCloudQueueMessage(QueueMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } //when there are no properties pack the data as binary in raw form if (message.Properties == null || message.Properties.Count == 0) { var r = new CloudQueueMessage((string)null); r.SetMessageContent2(message.Content); return(r); } //note that Azure Storage doesn't have properties on message, therefore I can do a simulation instead var clazz = new JsonProps { Properties = message.Properties.Select(p => new JsonProp { Name = p.Key, Value = p.Value }).ToArray() }; byte[] propBytes = Encoding.UTF8.GetBytes(clazz.ToJsonString()); CloudQueueMessage result; using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms, Encoding.UTF8)) { writer.Write(CustomFlagBytes); writer.Write(propBytes.Length); writer.Write(propBytes); if (message.Content != null) { writer.Write(message.Content); } } result = new CloudQueueMessage((string)null); result.SetMessageContent2(ms.ToArray()); } return(result); }