Message GenerateMessage(System.Messaging.Message message, string queuePath) { try { var headers = TryDeserializeHeaders(message); return(new Message { Label = message.Label, Time = message.ArrivedTime, Headers = headers, Bytes = TryDetermineMessageSize(message), Body = TryDecodeBody(message, headers), Id = message.Id, QueuePath = queuePath, }); } catch (Exception e) { return(new Message { Body = string.Format(@"Message could not be properly decoded: {0}", e) }); } }
Message GenerateMessage(System.Messaging.Message message, string queuePath) { try { Dictionary <string, string> headers; var couldDeserializeHeaders = TryDeserializeHeaders(message, out headers); string body; int bodySize; var couldDecodeBody = TryDecodeBody(message, headers, out body, out bodySize); return(new Message { Label = message.Label, Time = message.ArrivedTime, Headers = couldDeserializeHeaders ? new EditableDictionary <string, string>(headers) : new EditableDictionary <string, string>(), Bytes = bodySize, Body = body, Id = message.Id, QueuePath = queuePath, CouldDeserializeBody = couldDecodeBody, CouldDeserializeHeaders = couldDeserializeHeaders, }); } catch (Exception e) { return(new Message { Body = string.Format(@"Message could not be properly decoded: {0}", e) }); } }
static int GetLengthFromStreamIfPossible(System.Messaging.Message message) { try { return((int)message.BodyStream.Length); } catch { return(-1); } }
int TryDetermineMessageSize(System.Messaging.Message message) { try { return((int)message.BodyStream.Length); } catch (Exception e) { return(-1); } }
void EncodeBody(System.Messaging.Message message, Message messageModel) { var headers = messageModel.Headers; var body = messageModel.Body; if (headers.ContainsKey(Headers.ContentType)) { var encoding = headers[Headers.ContentType]; var encoder = GetEncoding(encoding); message.BodyStream = new MemoryStream(encoder.GetBytes(body)); } }
Dictionary <string, string> TryDeserializeHeaders(System.Messaging.Message message) { try { var headersAsJsonString = Encoding.UTF7.GetString(message.Extension); var headers = JsonConvert.DeserializeObject <Dictionary <string, string> >(headersAsJsonString); return(headers ?? new Dictionary <string, string>()); } catch { return(new Dictionary <string, string>()); } }
bool TryDeserializeHeaders(System.Messaging.Message message, out Dictionary <string, string> dictionary) { try { var headersAsJsonString = Encoding.UTF7.GetString(message.Extension); var headers = JsonConvert.DeserializeObject <Dictionary <string, string> >(headersAsJsonString); dictionary = headers; return(true); } catch { dictionary = null; return(false); } }
bool TryDecodeBody(System.Messaging.Message message, Dictionary <string, string> headers, out string body, out int bodySize) { try { if (headers == null) { body = "Message has no headers that can be understood by Rebus"; bodySize = GetLengthFromStreamIfPossible(message); return(false); } if (!headers.ContainsKey(Headers.ContentType)) { body = string.Format("Message headers don't contain an element with the '{0}' key", Headers.ContentType); bodySize = GetLengthFromStreamIfPossible(message); return(false); } var destination = new MemoryStream(); message.BodyStream.CopyTo(destination); var bytes = destination.ToArray(); var bodyIsGzipped = headers.ContainsKey(Headers.ContentEncoding) && string.Equals(headers[Headers.ContentEncoding], "gzip", StringComparison.InvariantCultureIgnoreCase); if (bodyIsGzipped) { bytes = new Zipper().Unzip(bytes); } var encoding = headers[Headers.ContentType]; var encoder = GetEncoding(encoding); var str = encoder.GetString(bytes); body = TryToIndentJson(str); bodySize = bytes.Length; return(true); } catch (Exception e) { body = string.Format("An error occurred while decoding the body: {0}", e); bodySize = GetLengthFromStreamIfPossible(message); return(false); } }
string TryDecodeBody(System.Messaging.Message message, Dictionary <string, string> headers) { if (headers.ContainsKey(Headers.Encoding)) { var encoding = headers[Headers.Encoding]; var encoder = Encoding.GetEncoding(encoding); using (var reader = new BinaryReader(message.BodyStream)) { var bytes = reader.ReadBytes((int)message.BodyStream.Length); var str = encoder.GetString(bytes); return(str); } } return("(message encoding not specified)"); }
bool TryDecodeBody(System.Messaging.Message message, Dictionary <string, string> headers, out string body) { if (headers.ContainsKey(Headers.Encoding)) { var encoding = headers[Headers.Encoding]; var encoder = Encoding.GetEncoding(encoding); using (var reader = new BinaryReader(message.BodyStream)) { var bytes = reader.ReadBytes((int)message.BodyStream.Length); var str = encoder.GetString(bytes); body = str; return(true); } } body = null; return(false); }
bool TryDecodeBody(System.Messaging.Message message, Dictionary <string, string> headers, out string body, out int bodySize) { try { if (headers == null) { body = "Message has no headers that can be understood by Rebus"; bodySize = GetLengthFromStreamIfPossible(message); return(false); } if (!headers.ContainsKey(Headers.ContentType)) { body = string.Format("Message headers don't contain an element with the '{0}' key", Headers.ContentType); bodySize = GetLengthFromStreamIfPossible(message); return(false); } var encoding = headers[Headers.ContentType]; var encoder = GetEncoding(encoding); using (var reader = new BinaryReader(message.BodyStream)) { var bytes = reader.ReadBytes((int)message.BodyStream.Length); var str = encoder.GetString(bytes); body = TryToIndentJson(str); bodySize = bytes.Length; return(true); } } catch (Exception e) { body = string.Format("An error occurred while decoding the body: {0}", e); bodySize = GetLengthFromStreamIfPossible(message); return(false); } }
void UpdateMessage(Message message, Queue queueToReload) { Task.Factory .StartNew(() => { if (!message.CouldDeserializeBody) { throw new InvalidOperationException( string.Format( "Body of message with ID {0} was not properly deserialized, so it's not safe to try to update it...", message.Id)); } using (var queue = new MessageQueue(message.QueuePath)) { queue.MessageReadPropertyFilter = LosslessFilter(); using (var transaction = new MessageQueueTransaction()) { transaction.Begin(); try { var msmqMessage = queue.ReceiveById(message.Id, transaction); var newMsmqMessage = new System.Messaging.Message { Label = msmqMessage.Label, Extension = msmqMessage.Extension, TimeToBeReceived = msmqMessage.TimeToBeReceived, UseDeadLetterQueue = msmqMessage.UseDeadLetterQueue, UseJournalQueue = msmqMessage.UseJournalQueue, }; EncodeBody(newMsmqMessage, message); queue.Send(newMsmqMessage, transaction); transaction.Commit(); } catch { transaction.Abort(); throw; } } } return(new { Message = message, Queue = queueToReload, Notification = NotificationEvent.Success("Fetched message with ID {0} and put an updated version back in the queue", message.Id), }); }) .ContinueWith(a => { if (a.Exception != null) { Messenger.Default.Send(NotificationEvent.Fail(a.Exception.ToString(), "Something went wrong while attempting to update message with ID {0}", message.Id)); return; } var result = a.Result; Messenger.Default.Send(new ReloadMessagesRequested(result.Queue)); Messenger.Default.Send(result.Notification); }, Context.UiThread); }
void UpdateMessage(Message message, Queue queueToReload) { Task.Factory .StartNew(() => { if (!message.CouldDeserializeBody) { throw new InvalidOperationException( string.Format( "Body of message with ID {0} was not properly deserialized, so it's not safe to try to update it...", message.Id)); } using (var queue = new MessageQueue(message.QueuePath)) { queue.MessageReadPropertyFilter = LosslessFilter(); using (var transaction = new MessageQueueTransaction()) { transaction.Begin(); try { var msmqMessage = queue.ReceiveById(message.Id, transaction); var newMsmqMessage = new System.Messaging.Message { Label = msmqMessage.Label, Extension = msmqMessage.Extension, TimeToBeReceived = msmqMessage.TimeToBeReceived, UseDeadLetterQueue = msmqMessage.UseDeadLetterQueue, UseJournalQueue = msmqMessage.UseJournalQueue, }; EncodeBody(newMsmqMessage, message); queue.Send(newMsmqMessage, transaction); transaction.Commit(); } catch { transaction.Abort(); throw; } } } return new { Message = message, Queue = queueToReload, Notification = NotificationEvent.Success("Fetched message with ID {0} and put an updated version back in the queue", message.Id), }; }) .ContinueWith(a => { if (a.Exception != null) { Messenger.Default.Send(NotificationEvent.Fail(a.Exception.ToString(), "Something went wrong while attempting to update message with ID {0}", message.Id)); return; } var result = a.Result; Messenger.Default.Send(new ReloadMessagesRequested(result.Queue)); Messenger.Default.Send(result.Notification); }, Context.UiThread); }