public MessageEnvelope(object message, Dictionary <string, object> customMetadata, MessageRaw raw)
        {
            Message         = message;
            _customMetadata = customMetadata;
            _raw            = raw;

            _raw.MessagePayload?.Dispose();
            _raw.MessagePayload = null;
            _raw.MessageMetadataPayload?.Dispose();
            _raw.MessageMetadataPayload = null;
        }
        private static object DeserializeMessage(ISerializer serializer, MessageRaw raw)
        {
            if (raw.MessagePayload == null)
            {
                return(null);
            }

            using (var reader = new StreamReader(raw.MessagePayload))
            {
                return(serializer.Deserialize(raw.MessageContractName, reader));
            }
        }
        private static Dictionary <string, object> DeserializeMetadata(ISerializer serializer, MessageRaw raw)
        {
            if (raw.MessageMetadataPayload == null)
            {
                return(null);
            }

            using (var reader = new StreamReader(raw.MessageMetadataPayload))
            {
                return(serializer.Deserialize <Dictionary <string, object> >(reader));
            }
        }
        public static MessageEnvelope Transform(ISerializer serializer, IUpConverterFactory upConverterFactory, MessageRaw raw)
        {
            var message        = DeserializeMessage(serializer, raw);
            var customMetadata = DeserializeMetadata(serializer, raw);

            if (upConverterFactory != null)
            {
                message = UpConvert(upConverterFactory, message.GetType(), message);
            }

            return(new MessageEnvelope(message, customMetadata, raw));
        }