public static BrokeredMessage ConvertAmqpToSbmp(BrokeredMessage brokeredMessage, ReceiveMode receiveMode) { BrokeredMessage encoder = brokeredMessage.CreateCopy(); encoder.MessageFormat = BrokeredMessageFormat.Sbmp; encoder.MessageEncoder = BrokeredMessageEncoder.GetEncoder(BrokeredMessageFormat.Sbmp); if (receiveMode == ReceiveMode.PeekLock && brokeredMessage.IsLockTokenSet) { encoder.LockToken = brokeredMessage.LockToken; encoder.LockedUntilUtc = brokeredMessage.LockedUntilUtc; } if (encoder.BodyStream != null) { using (Stream stream = BrokeredMessage.CloneStream(encoder.BodyStream, false)) { using (AmqpMessage amqpMessage = AmqpMessage.CreateAmqpStreamMessageBody(stream)) { encoder.BodyStream = null; if ((int)(amqpMessage.BodyType & SectionFlag.Data) != 0 || (int)(amqpMessage.BodyType & SectionFlag.AmqpSequence) != 0) { encoder.BodyStream = MessageConverter.GetMessageBodyStream(amqpMessage); } else if ((int)(amqpMessage.BodyType & SectionFlag.AmqpValue) != 0) { object value = null; if (!MessageConverter.TryGetNetObjectFromAmqpObject(amqpMessage.ValueBody.Value, MappingType.MessageBody, out value)) { value = amqpMessage.ValueBody.Value; } if (value != null) { DataContractBinarySerializer dataContractBinarySerializer = new DataContractBinarySerializer(value.GetType()); MemoryStream memoryStream = new MemoryStream(256); dataContractBinarySerializer.WriteObject(memoryStream, value); memoryStream.Flush(); memoryStream.Position = (long)0; encoder.BodyStream = memoryStream; } } } } } return(encoder); }