IndexOutOfRangeException Net8 SS8

After upgrading to .NET 8 and SS 8.01, I’m suddenly getting IndexOutOfRangeException for simple Select.

This code, which I’ve been using for a while:

    public List<Employee> GetEmployees()
    {
        using var db = DbFactory.OpenDbConnection();
        return db.Select<Employee>();
    }

Gives this output:

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at ServiceStack.OrmLite.OrmLiteUtils.HandleException(Exception ex, String message, Object[] args) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteUtils.cs:line 40
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.PopulateObjectWithSqlReader[T](IOrmLiteDialectProvider dialectProvider, Object objWithProperties, IDataReader reader, Tuple`3[] indexCache, Object[] values) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs:line 382
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.PopulateWithSqlReader[T](T objWithProperties, IOrmLiteDialectProvider dialectProvider, IDataReader reader, Tuple`3[] indexCache, Object[] values) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs:line 330
   at ServiceStack.OrmLite.OrmLiteUtils.ConvertToList[T](IDataReader reader, IOrmLiteDialectProvider dialectProvider, HashSet`1 onlyFields) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteUtils.cs:line 266
   at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ExprConvertToList[T](IDbCommand dbCmd, String sql, IEnumerable`1 sqlParams, HashSet`1 onlyFields) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteResultsFilterExtensions.cs:line 143
   at ServiceStack.OrmLite.ReadExpressionCommandExtensions.Select[T](IDbCommand dbCmd, SqlExpression`1 q) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/Expressions/ReadExpressionCommandExtensions.cs:line 15
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.<>c__DisplayClass34_0`1.<Select>b__0(IDbCommand dbCmd) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteReadExpressionsApi.cs:line 266
   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteExecFilter.cs:line 68
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func`2 filter) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteReadExpressionsApi.cs:line 18
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Select[T](IDbConnection dbConn, SqlExpression`1 expression) in /home/runner/work/ServiceStack/ServiceStack/ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteReadExpressionsApi.cs:line 266
   at  ... // continues with my code

I’ve used the BeforeExecFilter, copied the SQL to the same DB and I’m getting results from the query.

// to serialize complex objects as JSON instead of JSV
SqlServerDialect.Provider.StringSerializer = new JsonStringSerializer();
OrmLiteConfig.BeforeExecFilter = dbCmd => OrmLiteUtils.PrintSql();        // turn on for DEBUG

var connectionString = Configuration.GetConnectionString("DbConnectionString");
var dialectProvider = SqlServer2019Dialect.Provider; 

var dbFactory = new OrmLiteConnectionFactory(connectionString, dialectProvider);
container.Register<IDbConnectionFactory>(dbFactory);

It seems like adding Take() makes it work:

// crashes:

using var db = DbFactory.OpenDbConnection();
var q = db.From<Employee>();
return db.Select(q);

// works:

using var db = DbFactory.OpenDbConnection();
var q = db.From<Employee>().Take(5);
return db.Select(q);

There’s a certain “magic number” that the Take wants for each query to work.

For one query I was able to nail it down to the exact number.
For this sample, I didn’t bother, but somewhere between 750 and 800 there’s a number that works. And BTW there are 1697 rows total.

var q = db.From<Employee>().Take(700); // take 800 crashes (
return db.Select(q).ToList();

Again, the exception from this actual run:

   at ServiceStack.OrmLite.OrmLiteUtils.HandleException(Exception ex, String message, Object[] args)
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.PopulateObjectWithSqlReader[T](IOrmLiteDialectProvider dialectProvider, Object objWithProperties, IDataReader reader, Tuple`3[] indexCache, Object[] values)
   at ServiceStack.OrmLite.OrmLiteWriteCommandExtensions.PopulateWithSqlReader[T](T objWithProperties, IOrmLiteDialectProvider dialectProvider, IDataReader reader, Tuple`3[] indexCache, Object[] values)
   at ServiceStack.OrmLite.OrmLiteUtils.ConvertToList[T](IDataReader reader, IOrmLiteDialectProvider dialectProvider, HashSet`1 onlyFields)
   at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ExprConvertToList[T](IDbCommand dbCmd, String sql, IEnumerable`1 sqlParams, HashSet`1 onlyFields)
   at ServiceStack.OrmLite.ReadExpressionCommandExtensions.Select[T](IDbCommand dbCmd, SqlExpression`1 q)
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.<>c__DisplayClass34_0`1.<Select>b__0(IDbCommand dbCmd)
   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func`2 filter)
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Select[T](IDbConnection dbConn, SqlExpression`1 expression)
   at Masterdata.Repository.MasterdataRepository.GetEmployees() 

My version of dotnet:

8.0.100

On a final note, I downgraded to SS version “6.*” (from 8.0.1) and then it works without Take, like before.

I only downgraded SS, still targeting .net8. I deleted all bin/obj folders for the whole solution, did dotnet build and dotnet publish.

It’s likely because we’ve changed the default to ThrowOnError previously these field mapping errors would’ve only been logged, now it will throw by default.

You can revert back the previous behavior with:

OrmLiteConfig.ThrowOnError = false;

I’d assume Take() just removes the result that caused the Exception from the query, from your description the offending row is between rows 750-800 of that query.

If you can put together a small repro I’ll be able to identify what’s causing it.

BTW I’ve updated the code to rethrow the Exception so it will be better at identifying the source of the Exception, the error log should also specify which column had the issue.

You can download the latest v8.0.1 by clearing your NuGet packages cache then restoring packages again.

I’m not seeing the error after clearing the cache and updating again. Will investigate further when I have time.