Esempio n. 1
0
        /// <summary>
        /// Sets the body.
        /// </summary>
        /// <param name="serializedMessage">The serialized message.</param>
        /// <param name="body">The body.</param>
        /// <returns>The <see cref="SerializableMessage"/></returns>
        public static SerializableMessage SetBody(this SerializableMessage serializedMessage, object body)
        {
            if (serializedMessage == null)
            {
                throw new ArgumentNullException("serializedMessage");
            }

            lock (serializedMessage)
            {
                // create an instance of the message formatter supplied in the serialized message.
                IMessageFormatter messageFormatter = serializedMessage.GetFormatter();

                // in order to ensure that the message is written to the MessageData property correctly,
                // use the message formatter to write to a new Message object, then extract the serialized
                // data from the Message.BodyStream.
                using (Message message = new Message())
                {
                    // setup the message object...
                    message.Formatter = messageFormatter;
                    message.Body      = body;

                    // use the formatter to write the object to the message
                    // this populates the message.BodyStream property.
                    messageFormatter.Write(message, body);

                    // reset the stream position to zero just in case the message formatter neglects to do so...
                    message.BodyStream.Position = 0;

                    serializedMessage.MessageData = StreamUtility.ReadFully(message.BodyStream);

                    return(serializedMessage);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Converts the <see cref="SerializableMessage"/> to a <see cref="Message"/>.
        /// </summary>
        /// <param name="serializedMessage">The serialized message.</param>
        /// <param name="setBody">if set to <c>true</c> set <see cref="Message.Body"/> property to the deserialized object.</param>
        /// <returns>A <see cref="Message"/></returns>
        public static Message AsMessage(this SerializableMessage serializedMessage, bool setBody = false)
        {
            if (serializedMessage == null)
            {
                throw new ArgumentNullException("serializedMessage");
            }


            // use two message variables to satisfy CA for disposing objects on exception paths....
            Message message       = null;
            Message returnMessage = null;

            try
            {
                lock (serializedMessage)
                {
                    /////////////////////////////////////////////////////////////////////////////
                    // to convert to a Sytem.Messaging.Message:
                    // 1. create a new Message object
                    // 2. set the formatter
                    // 3. copy serialized properties
                    // 4. get the object "body" from the serialized message
                    // 5. set the body to the message and use the formatter to write it
                    //    so that the BodyStream is populated.

                    message               = new Message();
                    message.Formatter     = serializedMessage.GetFormatter();
                    message.CorrelationId = serializedMessage.CorrelationId;
                    message.Label         = serializedMessage.Label;
                    message.AppSpecific   = serializedMessage.AppSpecific;
                    message.Priority      = (MessagePriority)serializedMessage.MessagePriority;
                    message.Extension     = serializedMessage.Extension;

                    if (setBody)
                    {
                        object body = serializedMessage.GetBody();
                        message.Body = body;
                        message.Formatter.Write(message, body);
                    }
                    else
                    {
                        message.BodyStream = new MemoryStream(serializedMessage.MessageData);
                    }
                    /////////////////////////////////////////////////////////////////////////////
                }

                returnMessage = message;
                message       = null;

                return(returnMessage);
            }
            finally
            {
                if (message != null)
                {
                    message.Dispose();
                }
            }
        }