MessageReadResult ExecuteReader(SqlCommand command) { object[] rowData; using (var dataReader = command.ExecuteReader(CommandBehavior.SingleRow)) { if (dataReader.Read()) { rowData = new object[dataReader.FieldCount]; // ReSharper disable once ReturnValueOfPureMethodIsNotUsed dataReader.GetValues(rowData); } else { return(MessageReadResult.NoMessage); } } try { var id = rowData[0].ToString(); int?millisecondsToExpiry = null; if (rowData[TimeToBeReceivedColumn] != DBNull.Value) { millisecondsToExpiry = (int)rowData[TimeToBeReceivedColumn]; } //Has message expired? if (millisecondsToExpiry.HasValue && millisecondsToExpiry.Value < 0L) { Logger.InfoFormat("Message with ID={0} has expired. Removing it from queue.", id); return(MessageReadResult.NoMessage); } var headers = (Dictionary <string, string>)HeaderSerializer.DeserializeObject((string)rowData[HeadersColumn], typeof(Dictionary <string, string>)); var correlationId = GetNullableValue <string>(rowData[CorrelationIdColumn]); var recoverable = (bool)rowData[RecoverableColumn]; var body = GetNullableValue <byte[]>(rowData[BodyColumn]); var message = new TransportMessage(id, headers) { CorrelationId = correlationId, Recoverable = recoverable, Body = body ?? new byte[0] }; var replyToAddress = GetNullableValue <string>(rowData[ReplyToAddressColumn]); if (!string.IsNullOrEmpty(replyToAddress)) { message.Headers[Headers.ReplyToAddress] = replyToAddress; } if (millisecondsToExpiry.HasValue) { message.TimeToBeReceived = TimeSpan.FromMilliseconds(millisecondsToExpiry.Value); } return(MessageReadResult.Success(message)); } catch (Exception ex) { Logger.Error("Error receiving message. Probable message metadata corruption. Moving to error queue.", ex); return(MessageReadResult.Poison(rowData)); } }