public RangeBlock GetLastRangeBlock(LastBlockRequest lastRangeBlockRequest)
        {
            var taskDefinition = _taskRepository.EnsureTaskDefinition(lastRangeBlockRequest.TaskId);

            var query = string.Empty;
            if (lastRangeBlockRequest.BlockType == BlockType.DateRange)
                query = RangeBlockQueryBuilder.GetLastDateRangeBlock(lastRangeBlockRequest.LastBlockOrder);
            else if (lastRangeBlockRequest.BlockType == BlockType.NumericRange)
                query = RangeBlockQueryBuilder.GetLastNumericRangeBlock(lastRangeBlockRequest.LastBlockOrder);
            else
                throw new ArgumentException("An invalid BlockType was supplied: " + lastRangeBlockRequest.BlockType);

            try
            {
                using (var connection = CreateNewConnection(lastRangeBlockRequest.TaskId))
                {
                    var command = connection.CreateCommand();
                    command.CommandText = query;
                    command.CommandTimeout = ConnectionStore.Instance.GetConnection(lastRangeBlockRequest.TaskId).QueryTimeoutSeconds;
                    command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinition.TaskDefinitionId;
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var rangeBlockId = reader["BlockId"].ToString();
                            long rangeBegin;
                            long rangeEnd;

                            if (lastRangeBlockRequest.BlockType == BlockType.DateRange)
                            {
                                rangeBegin = reader.GetDateTime(2).Ticks; //DateTime.Parse(reader["FromDate"].ToString()).Ticks; 
                                rangeEnd = reader.GetDateTime(3).Ticks; //DateTime.Parse(reader["ToDate"].ToString()).Ticks;
                            }
                            else
                            {
                                rangeBegin = long.Parse(reader["FromNumber"].ToString());
                                rangeEnd = long.Parse(reader["ToNumber"].ToString());
                            }

                            return new RangeBlock(rangeBlockId, 0, rangeBegin, rangeEnd, lastRangeBlockRequest.BlockType);
                        }
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                if (TransientErrorDetector.IsTransient(sqlEx))
                    throw new TransientException("A transient exception has occurred", sqlEx);

                throw;
            }

            return null;
        }
        private LastBlockRequest CreateRequest(LastBlockOrder lastBlockOrder)
        {
            var request = new LastBlockRequest(new TaskId(TestConstants.ApplicationName, TestConstants.TaskName), BlockType.NumericRange);
            request.LastBlockOrder = lastBlockOrder;

            return request;
        }
        private LastBlockRequest CreateRequest()
        {
            var request = new LastBlockRequest(new TaskId(TestConstants.ApplicationName, TestConstants.TaskName), BlockType.Object);
            request.LastBlockOrder = LastBlockOrder.LastCreated;

            return request;
        }
        public ProtoListBlock GetLastListBlock(LastBlockRequest lastRangeBlockRequest)
        {
            var taskDefinition = _taskRepository.EnsureTaskDefinition(lastRangeBlockRequest.TaskId);

            try
            {
                using (var connection = CreateNewConnection(lastRangeBlockRequest.TaskId))
                {
                    var command = connection.CreateCommand();
                    command.CommandText = ListBlockQueryBuilder.GetLastListBlock;
                    command.CommandTimeout = ConnectionStore.Instance.GetConnection(lastRangeBlockRequest.TaskId).QueryTimeoutSeconds;
                    command.Parameters.Add("@TaskDefinitionId", SqlDbType.Int).Value = taskDefinition.TaskDefinitionId;
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var listBlock = new ProtoListBlock();
                            listBlock.ListBlockId = reader["BlockId"].ToString();
                            listBlock.Items = GetListBlockItems(lastRangeBlockRequest.TaskId, listBlock.ListBlockId);
                            listBlock.Header = SerializedValueReader.ReadValueAsString(reader, "ObjectData", "CompressedObjectData");

                            return listBlock;
                        }
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                if (TransientErrorDetector.IsTransient(sqlEx))
                    throw new TransientException("A transient exception has occurred", sqlEx);

                throw;
            }

            return null;
        }