/// <summary>
        /// Alternative version that selects & updates the row in a single query.
        /// However, the testing has shown that it's actually slower than the original, two-query, version
        /// I'm leaving it here to remember that this has already been tried and failed.
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="retryTime"></param>
        /// <returns></returns>
        private MessageContainer SelectNextMessageForProcessing2008(IDbConnection conn, out DateTime?retryTime)
        {
            var mc = new MessageContainer();

            retryTime = null;
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = string.Format(@"UPDATE TOP(1) {0} with(readpast)
                    SET subqueue = 'X', last_processed = getdate()
                    OUTPUT
                     inserted.id, inserted.from_endpoint, inserted.to_endpoint, inserted.retry_count, inserted.retry_time, inserted.correlation_id, inserted.msg_text, inserted.msg_headers, inserted.unique_id
                    WHERE
                    id in (select top(1) id from {0} with(readpast) where subqueue = 'I' order by retry_time)", _queueTable);
                using (var dr = cmd.ExecuteReader())
                {
                    if (!dr.Read())
                    {
                        return(null);
                    }
                    mc.From          = Convert.ToString(dr["from_endpoint"]);
                    mc.To            = Convert.ToString(dr["to_endpoint"]);
                    mc.HeadersString = Convert.ToString(dr["msg_headers"]);
                    mc.SetHeader(MessageContainer.HDR_RetryCount, Convert.ToInt32(dr["retry_count"]).ToString());;
                    mc.CorrelationId = Convert.ToString(dr["correlation_id"]);
                    mc.BusMessageId  = Convert.ToString(dr["id"]);
                    mc.UniqueId      = Convert.ToString(dr["unique_id"]);
                    retryTime        = Convert.ToDateTime(dr["retry_time"]);
                    mc.BodyStr       = dr.GetString(dr.GetOrdinal("msg_text"));
                }
            }
            return(mc);
        }
        public string Serialize(MessageContainer mc)
        {
            if (string.IsNullOrEmpty(mc.BodyStr))
            {
                if (mc.Body != null)
                {
                    var sw = new StringWriter();
                    _ser.Serialize(sw, mc.Body);
                    if (!EmbedMessageTypeInBody)
                    {
                        mc.SetHeader(MessageContainer.HDR_MessageType, TypeToString(mc.Body.GetType()));
                    }
                    mc.BodyStr = sw.ToString();
                    mc.Body    = null;
                }
            }
            var sw2 = new StringWriter();

            _ser.Serialize(sw2, mc);
            return(sw2.ToString());
        }
        public string Serialize(MessageContainer mc)
        {
            if (string.IsNullOrEmpty(mc.BodyStr))
            {
                if (mc.Body != null)
                {

                    var sw = new StringWriter();
                    _ser.Serialize(sw, mc.Body);
                    if (!EmbedMessageTypeInBody)
                    {
                        mc.SetHeader(MessageContainer.HDR_MessageType, TypeToString(mc.Body.GetType()));
                    }
                    mc.BodyStr = sw.ToString();
                    mc.Body = null;
                }
            }
            var sw2 = new StringWriter();
            _ser.Serialize(sw2, mc);
            return sw2.ToString();
        }
        protected MessageContainer Unwrap(MongoMessageWrapper m)
        {
            var mc = new MessageContainer();
            mc.Headers = new Dictionary<string, string>();
            if (m.Headers != null)
            {
                foreach (string s in m.Headers.Keys)
                {
                    mc.SetHeader(s, m.Headers[s]);
                }
            }

            mc.BodyStr = m.Payload;
            mc.BusMessageId = m._id.ToString();
            mc.RetryCount = m.RetryCount;
            mc.From = m.From;
            mc.To = m.To;

            return mc;
        }
 public ISendMessage AddHeader(string name, string value)
 {
     mc.SetHeader(name, value);
     return(this);
 }
 /// <summary>
 /// Alternative version that selects & updates the row in a single query.
 /// However, the testing has shown that it's actually slower than the original, two-query, version
 /// I'm leaving it here to remember that this has already been tried and failed.
 /// </summary>
 /// <param name="conn"></param>
 /// <param name="retryTime"></param>
 /// <returns></returns>
 private MessageContainer SelectNextMessageForProcessing2008(IDbConnection conn, out DateTime? retryTime)
 {
     var mc = new MessageContainer();
     retryTime = null;
     using (var cmd = conn.CreateCommand())
     {
         cmd.CommandText = string.Format(@"UPDATE TOP(1) {0} with(readpast)
             SET subqueue = 'X', last_processed = getdate()
             OUTPUT
              inserted.id, inserted.from_endpoint, inserted.to_endpoint, inserted.retry_count, inserted.retry_time, inserted.correlation_id, inserted.msg_text, inserted.msg_headers, inserted.unique_id
             WHERE
             id in (select top(1) id from {0} with(readpast) where subqueue = 'I' order by retry_time)", _queueTable);
         using (var dr = cmd.ExecuteReader())
         {
             if (!dr.Read()) return null;
             mc.From = Convert.ToString(dr["from_endpoint"]);
             mc.To = Convert.ToString(dr["to_endpoint"]);
             mc.HeadersString = Convert.ToString(dr["msg_headers"]);
             mc.SetHeader(MessageContainer.HDR_RetryCount, Convert.ToInt32(dr["retry_count"]).ToString()); ;
             mc.CorrelationId = Convert.ToString(dr["correlation_id"]);
             mc.BusMessageId = Convert.ToString(dr["id"]);
             mc.UniqueId = Convert.ToString(dr["unique_id"]);
             retryTime = Convert.ToDateTime(dr["retry_time"]);
             mc.BodyStr = dr.GetString(dr.GetOrdinal("msg_text"));
         }
     }
     return mc;
 }