DapperLib / DapperLib/Dapper

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

Open
#2,135 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-triage
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

We encountered an issue retrieving DynamicParameters using the Get method after the QueryAsync 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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at DynamicParameters.cs line 329 and trace how QueryAsync populates output parameters before Get is called. Reproduce the supplied stored-procedure example with Microsoft.Data.SqlClient 5.2.0 and Dapper 2.0.123; done means identifying the intermittent null path and resolving or clearly documenting the cause.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.