/// <summary>
        /// Serializes and assigns a <see cref="MessageRetryData"/> instance to the Extension
        /// property of a <see cref="Message"/>.
        /// </summary>
        /// <param name="message">The object on which the Extension property will be set.</param>
        /// <param name="messageRetryData">The object that will be serialized and written to the Extension property.</param>
        /// <returns>The <see cref="SerializedMessage"/> instance after the <see cref="MessageRetryData"/> instance has been serialized and assigned to its Extension property.</returns>
        public static Message SetRetryData(this Message message, MessageRetryData messageRetryData)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (messageRetryData == null)
            {
                throw new ArgumentNullException("messageRetryData");
            }

            message.Extension = ConvertToBytes(messageRetryData);

            return(message);
        }
        private static byte[] ConvertToBytes(MessageRetryData messageRetryData)
        {
            byte[] bytes;

            using (var memoryStream = new MemoryStream())
            {
                var serializer = new DataContractSerializer(typeof(MessageRetryData));

                serializer.WriteObject(memoryStream, messageRetryData);

                bytes = memoryStream.ToArray();
            }

            return(bytes);
        }
        private static MessageRetryData ConvertToObject(byte[] bytes)
        {
            MessageRetryData messageRetryData = null;

            using (var memoryStream = new MemoryStream(bytes))
            {
                var serializer = new DataContractSerializer(typeof(MessageRetryData));

                if (memoryStream.Length != 0)
                {
                    messageRetryData = serializer.ReadObject(memoryStream) as MessageRetryData;
                }
            }

            return(messageRetryData);
        }
        /// <summary>
        /// Determines whether this instance can process the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>
        ///     <c>true</c> if this instance can the specified message; otherwise, <c>false</c>.
        /// </returns>
        public bool CanProcessMessage(SerializableMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            try
            {
                object body = message.GetBody();

                if (body == null)
                {
                    return(false);
                }

                if (!this._handledTypes.Contains(body.GetType()))
                {
                    return(false);
                }

                MessageRetryData messageRetryData = message.GetRetryData();

                if (messageRetryData.Attempts == 1)
                {
                    return(true);
                }

                if (messageRetryData.RetrySourceTypeName != this.GetType().FullName)
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception exception)
            {
                return(false);
            }
        }
        /// <summary>
        /// Serializes and assigns a <see cref="MessageRetryData"/> instance to the Extension
        /// property of a <see cref="SerializableMessage"/>.
        /// </summary>
        /// <param name="serializableMessage">The object on which the Extension property will be set.</param>
        /// <param name="messageRetryData">The object that will be serialized and written to the Extension property.</param>
        /// <returns>The <see cref="SerializedMessage"/> instance after the <see cref="MessageRetryData"/> instance has been serialized and assigned to its Extension property.</returns>
        public static SerializableMessage SetRetryData(this SerializableMessage serializableMessage, MessageRetryData messageRetryData)
        {
            if (serializableMessage == null)
            {
                throw new ArgumentNullException("serializableMessage");
            }
            if (messageRetryData == null)
            {
                throw new ArgumentNullException("messageRetryData");
            }

            serializableMessage.Extension = ConvertToBytes(messageRetryData);

            return(serializableMessage);
        }