/// <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(); } } }
/// <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> /// Gets the body. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serializedMessage">The serialized message.</param> /// <returns>The message body as <typeparamref name="T"/></returns> public static T GetBody <T>(this SerializableMessage serializedMessage) { return((T)serializedMessage.GetBody()); }