Beispiel #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);
                }
            }
        }
Beispiel #2
0
        public static SerializableMessage SetBodyStream(this SerializableMessage serializedMessage, Stream bodyStream)
        {
            if (serializedMessage == null)
            {
                throw new ArgumentNullException("serializedMessage");
            }

            if (bodyStream == null)
            {
                throw new ArgumentNullException("bodyStream");
            }

            lock (serializedMessage)
            {
                lock (bodyStream)
                {
                    bodyStream.Position           = 0;
                    serializedMessage.MessageData = StreamUtility.ReadFully(bodyStream);
                    return(serializedMessage);
                }
            }
        }