You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When migrating from System.Data.SqlClient to Microsoft.Data.SqlClient i stumbled on a strange behavior.
I'm using IDbConnection.ExecuteAsync(new CommandDefinition(...parameters: IEnumerable<T>, flags: CommandFlags.Pipelined)) on a few places in my code.
Works fine with System.Data.SqlClient but fails with Microsoft.Data.SqlClient. The exception i get is:
"System.InvalidOperationException: The method 'EndExecuteNonQuery' cannot be called more the once for the.."
I've looked in to the code, might found the problem, and tried a solution. The current state of the Task management when running async pipline mode with multiple parameters is described in short below. full version here
Foreach param, create a command and =>
var task = cmd.ExecuteNonQueryAsync(command.CancellationToken);
pending.Enqueue(new AsyncExecState(cmd, task));
When creation of tasks based on multiple params is done =>
while (pending.Count != 0)
{
var pair = pending.Dequeue();
using (pair.Command) { /* dispose commands */ }
total += await pair.Task.ConfigureAwait(false);
}
The exception seems to arise when the pair.Command is disposed and then the pair.Task is awaited.
What seems to fix the problem is to infer the using of the executing command with the corresponding work looking something like e.g:
Command creation based on multiple params =>
async Task<int> ExecuteQueryAsync(DbCommand cmd)
{
using (cmd)
{
return await cmd.ExecuteNonQueryAsync(command.CancellationToken);
}
}
var task = ExecuteQueryAsync(cmd);
pending.Enqueue(task);
Continuation =>
while (pending.Count != 0)
{
var task = pending.Dequeue();
total += await task.ConfigureAwait(false);
}
I'm to novice to get an overview on how this affects the management of the commands, and why it was splitted in the first place. I've tested it locally and the happy path works, but have not tested it extensivly.
The text was updated successfully, but these errors were encountered:
When migrating from System.Data.SqlClient to Microsoft.Data.SqlClient i stumbled on a strange behavior.
I'm using
IDbConnection.ExecuteAsync(new CommandDefinition(...parameters: IEnumerable<T>, flags: CommandFlags.Pipelined))
on a few places in my code.Works fine with System.Data.SqlClient but fails with Microsoft.Data.SqlClient. The exception i get is:
"System.InvalidOperationException: The method 'EndExecuteNonQuery' cannot be called more the once for the.."
I've looked in to the code, might found the problem, and tried a solution. The current state of the Task management when running async pipline mode with multiple parameters is described in short below. full version here
Foreach param, create a command and =>
When creation of tasks based on multiple params is done =>
The exception seems to arise when the
pair.Command
is disposed and then thepair.Task
is awaited.What seems to fix the problem is to infer the using of the executing command with the corresponding work looking something like e.g:
Command creation based on multiple params =>
Continuation =>
I'm to novice to get an overview on how this affects the management of the commands, and why it was splitted in the first place. I've tested it locally and the happy path works, but have not tested it extensivly.
The text was updated successfully, but these errors were encountered: