Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

DynamicParameters retrieval failing with NullReferenceException after moving to Microsoft.Data.SqlClient

Open Pashok13 opened this issue 1 year ago • 0 comments

We encountered an issue retrieving DynamicParameters using the Get<T> method after the QueryAsync<T> method was finished. 
The stored procedure always returns the parameters we are looking for. Most of the calls are working fine, but still, the issue appears for less than 1% of cases with the next call stack:

System.NullReferenceException: Object reference not set to an instance of an object.  
at Dapper.DynamicParameters.Get[T](String name) in /_/Dapper/DynamicParameters.cs:line 329

This issue has been started right after moving from System.Data.SqlClient to Microsoft.Data.SqlClient

Code example:

using (var connection = new SqlConnection(_connectionString))
{
    await _asyncRetryPolicy.ExecuteAsync(() => connection.OpenAsync());

    DynamicParameters queryParameters = new DynamicParameters();
    queryParameters.Add(name: ERROR_CODE_PARAMETER_NAME, value: null, dbType: DbType.Int32, direction: ParameterDirection.Output);  
    queryParameters.Add(name: ERROR_MESSAGE_PARAMETER_NAME, value: "Unknown error", dbType: DbType.AnsiString, direction: ParameterDirection.Output, size: ERROR_MESSAGE_LEN);  

    var result = await connection.QueryAsync<T>(storedProcedureName, queryParameters, commandType: CommandType.StoredProcedure, commandTimeout: commandTimeout);

    int errorCode = queryParameters.Get<int>(ERROR_CODE_PARAMETER_NAME); // NullReference here
}

Could you please help us troubleshoot this issue?
Thank you.

Additional context Microsoft.Data.SqlClient v.5.2.0
Dapper v.2.0.123

Pashok13 avatar Dec 10 '24 13:12 Pashok13