System.IndexOutOfRangeException inside GetDapperRowDeserializer method
Hi, We faced with "Index was outside the bounds of the array." exception inside Dapper library. Unfortunately, we don't have many details for reproducing step-by-step, it was reproduced only once on our azure prod environment (so far). Below what we extracted in logs:
Version: Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
Stack Trace:
0
{"assembly":"Microsoft.Data.SqlClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5","method":"Microsoft.Data.SqlClient.SqlDataReader.CheckDataIsReady","level":0,"line":0}
1
{"assembly":"Microsoft.Data.SqlClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5","method":"Microsoft.Data.SqlClient.SqlDataReader.GetValueInternal","level":1,"line":0}
2
{"assembly":"Microsoft.Data.SqlClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5","method":"Microsoft.Data.SqlClient.SqlDataReader.GetValue","level":2,"line":0}
3
{"assembly":"Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null","method":"Dapper.SqlMapper+<>c__DisplayClass171_0.<GetDapperRowDeserializer>b__0","level":3**,"line":1909,"fileName":"/_/Dapper/SqlMapper.cs**"}
4
{"assembly":"Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null","method":"Dapper.SqlMapper+GridReader+<ReadDeferred>d__52`1.MoveNext","level":4,"line":360,"fileName":"/_/Dapper/SqlMapper.GridReader.cs"}
5
{"assembly":"System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Collections.Generic.List`1..ctor","level":5,"line":0}
6
{"assembly":"System.Linq, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Linq.Enumerable.ToList","level":6,"line":0}
7
{"assembly":"Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null","method":"Dapper.SqlMapper+GridReader.ReadImpl","level":7,"line":164,"fileName":"/_/Dapper/SqlMapper.GridReader.cs"}
С# code
using (SqlMapper.GridReader grid = await conn.QueryMultipleAsync(
sql: MyProcName,
param: parameters,
transaction: tran,
commandType: CommandType.StoredProcedure))
{
var expectedResultSetCount = 2;
var results = new List<IEnumerable<dynamic>>();
for (int i = 0; i < expectedResultSetCount; i++)
{
results.Add(grid.Read<dynamic>());
}
}
Error raised in strange place, when reader.FieldCount != real amount of columns in reader. File: https://github.com/DapperLib/Dapper/blob/main/Dapper/SqlMapper.cs Line: 1909
Can you please confirm if there is any chance to fix this?
Can you please confirm if there is any chance to fix this?
Without a concrete repro? Very little. I haven't seen this outcome, and I can't picture what would lead to the column count being wrong - I'd love to find out, but : a runnable example that showed it happening would be a great route in.
Can you please confirm if there is any chance to fix this?
Without a concrete repro? Very little. I haven't seen this outcome, and I can't picture what would lead to the column count being wrong - I'd love to find out, but : a runnable example that showed it happening would be a great route in.
Thank you for fast answer. We will track this issue on our envs in further and in case of repeatable reproducing will add additional info here.
Hi @mgravell and @Bazist
As I can see the code and the exception. I can tell if the grid is empty or null, then when the code tries to read the result at line: results.Add(grid.Read
Then an index out-of-bounds exception will be thrown. There can be many reasons due to which the grid might be null or empty, So you have to handle this scenario by doing a null or empty check.
I hope this is helpful
Hi @asherborntofly, It is not case of empty or null of the grid. In this case we would have regular "Object not set to an instance of an object" exception. It is raised inside Dapper library as unexpected case. Here, small explanation: File: https://github.com/DapperLib/Dapper/blob/main/Dapper/SqlMapper.cs Line: 1909 We read reader.FieldCount, but a little bit later reader.GetValue(i) raised error (i < reader.FieldCount).

Very similar that it is a multi-thread error or another occasionally error with lost connection. I suppose, that we don't need do anything so far, because in our logs we still don't have repeatable reproducing of this error.
Thanks @Bazist for your reply.
Can you share the log of this exception where I can find the error and stack trace?
@asherborntofly Sure, from first post:
0
{"assembly":"Microsoft.Data.SqlClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5","method":"Microsoft.Data.SqlClient.SqlDataReader.CheckDataIsReady","level":0,"line":0}
1
{"assembly":"Microsoft.Data.SqlClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5","method":"Microsoft.Data.SqlClient.SqlDataReader.GetValueInternal","level":1,"line":0}
2
{"assembly":"Microsoft.Data.SqlClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5","method":"Microsoft.Data.SqlClient.SqlDataReader.GetValue","level":2,"line":0}
3
{"assembly":"Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null","method":"Dapper.SqlMapper+<>c__DisplayClass171_0.<GetDapperRowDeserializer>b__0","level":3**,"line":1909,"fileName":"/_/Dapper/SqlMapper.cs**"}
4
{"assembly":"Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null","method":"Dapper.SqlMapper+GridReader+<ReadDeferred>d__52`1.MoveNext","level":4,"line":360,"fileName":"/_/Dapper/SqlMapper.GridReader.cs"}
5
{"assembly":"System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Collections.Generic.List`1..ctor","level":5,"line":0}
6
{"assembly":"System.Linq, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Linq.Enumerable.ToList","level":6,"line":0}
7
{"assembly":"Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null","method":"Dapper.SqlMapper+GridReader.ReadImpl","level":7,"line":164,"fileName":"/_/Dapper/SqlMapper.GridReader.cs"}