Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Pashok13 opened this issue Dec 10, 2024 · 0 comments

Comments

@Pashok13
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant