public async Task <object> GetBody(BrokeredMessage message)
        {
            var bodyType = GetBodyType(message);

            byte[] bodyBytes;

            object blobId;

            if (message.Properties.TryGetValue(MessagePropertyKeys.LargeBodyBlobIdentifier, out blobId))
            {
                bodyBytes = await _largeMessageBodyStore.Retrieve((string)blobId);
            }
            else
            {
                // Yep, this will actually give us the body Stream instead of trying to deserialize the body... cool API bro!
                using (var dataStream = message.GetBody <Stream>())
                    using (var memoryStream = new MemoryStream())
                    {
                        dataStream.CopyTo(memoryStream);
                        bodyBytes = memoryStream.ToArray();
                    }
            }

            var decompressedBytes = _compressor.Decompress(bodyBytes);
            var deserialized      = _serializer.Deserialize(Encoding.UTF8.GetString(decompressedBytes), bodyType);

            return(deserialized);
        }
Beispiel #2
0
        public async Task <NimbusMessage> DeserializeNimbusMessage(BrokeredMessage message)
        {
            byte[] compressedBytes;

            object blobId;
            string storageKey     = null;
            var    isLargeMessage = message.Properties.TryGetValue(MessagePropertyKeys.LargeBodyBlobIdentifier, out blobId);

            if (isLargeMessage)
            {
                storageKey      = (string)blobId;
                compressedBytes = await _largeMessageBodyStore.Retrieve(storageKey);
            }
            else
            {
                compressedBytes = message.GetBody <byte[]>();
            }

            var serializedBytes  = _compressor.Decompress(compressedBytes);
            var serializedString = Encoding.UTF8.GetString(serializedBytes);
            var deserialized     = _serializer.Deserialize(serializedString, typeof(NimbusMessage));
            var nimbusMessage    = (NimbusMessage)deserialized;

            if (!(nimbusMessage.Payload is IBusEvent))
            {
                if (isLargeMessage)
                {
                    await _largeMessageBodyStore.Delete(storageKey);
                }
            }

            return(nimbusMessage);
        }