DapperLib / DapperLib/DapperAOT
ExecuteScalarAsync calling stored procedure sp_set_session_context throws ArgumentNullException
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 472
- Forks
- 43
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 18
Description
Describe the bug
I am using .Net 8, Dapper 2.1.24 and Dapper.AOT 1.0.31 with SQL Server 2019 LocalDb.
I am trying to set session context variables by calling the system stored procedure sp_set_session_context - https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-set-session-context-transact-sql?view=sql-server-ver16
When calling ExecuteScalarAsync, an ArgumentNullException is thrown when using AOT but does not throw when not using AOT. The method below reproduces the bug.
[DapperAot]
public async Task SetSessionContextAsync()
{
// instantiate connection
string connectionString = ...
SqlConnection conn = new SqlConnection(connectionString);
// session context keys and values
Dictionary<string, object?>? sessionContext = new Dictionary<string, object?>()
{
{ "abc", 123 },
{ "def", "ghi" },
};
foreach (KeyValuePair<string, object?> kvp in sessionContext)
{
// this throws System.ArgumentNullException : Value cannot be null. (Parameter 'value')
_ = await conn.ExecuteScalarAsync<int>("sp_set_session_context", param: new { key = kvp.Key, value = kvp.Value }, commandType: CommandType.StoredProcedure);
}
}
Without [DapperAot] attribute or if the attribute is set to [DapperAot(false)] then ExecuteScalarAsync does not throw.
Here is the top of the stack trace excluding the calls from my test app.
CommandUtils.ThrowNull() line 86
Command`1.ExecuteScalarAsync[T](TArgs args, CancellationToken cancellationToken) line 73
Command`1.ExecuteScalarAsync[T](TArgs args, CancellationToken cancellationToken) line 77
...
Expected behavior
ExecuteScalarAsync should not throw.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with CommandUtils.ThrowNull at line 86 and Command.ExecuteScalarAsync at lines 73 and 77, then run the supplied SQL Server LocalDb reproduction with and without DapperAot. Done means the stored procedure call no longer throws under AOT while retaining the existing non-AOT behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100