From 1f861444853e6a1a9bb5cd6250dc53602daf4d58 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:02:54 -0800 Subject: [PATCH 1/3] Fix: ExpressiveCommand asks for a procedure's parameter names asynchronously An asynchronous run of a stored procedure on a command made over an IDbConnection that is a DbConnection looked the parameter names up with the blocking GetSchema call, on the async thread and without observing the cancellation token. ExpressiveCommand now overrides the asynchronous GetProcedureParameterNamesAsync as ExpressiveDbCommand does, so the lookup uses GetSchemaAsync on net10.0 and net11.0 (the netstandard targets keep the synchronous lookup, as before). Cost: first use of a procedure per connection only, nothing per call or row. Not user-visible beyond not blocking, so no release-notes line. Tests: an ExpressiveCommand over a DbConnection held as IDbConnection records GetSchemaAsync on the async path and GetSchema on the sync path. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/ExpressiveCommand.cs | 15 ++++++ Source/Core/PublicAPI.Unshipped.txt | 1 + .../ProcedureSchemaNamesTests.cs | 47 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Source/Core/ExpressiveCommand.cs b/Source/Core/ExpressiveCommand.cs index 88034c81..fd45f4e8 100644 --- a/Source/Core/ExpressiveCommand.cs +++ b/Source/Core/ExpressiveCommand.cs @@ -125,6 +125,21 @@ public ExpressiveCommand( protected override ImmutableArray GetProcedureParameterNames(IDbConnection connection, string procedure) => connection is DbConnection db ? ProcedureSchemaNames.Get(db, procedure) : default; + /// + /// Asynchronously gets the parameter names a stored procedure declares + /// through the ADO.NET schema collection ProcedureParameters when the + /// connection is a , so the lookup does not block + /// the calling thread. + /// + /// An open connection to run the lookup on. + /// The procedure name as given to the command. + /// A token that can cancel the lookup. + /// + protected override ValueTask> GetProcedureParameterNamesAsync(IDbConnection connection, string procedure, CancellationToken cancellationToken) + => connection is DbConnection db + ? ProcedureSchemaNames.GetAsync(db, procedure, cancellationToken) + : default; + /// protected override void AddParams(IDbCommand command) { diff --git a/Source/Core/PublicAPI.Unshipped.txt b/Source/Core/PublicAPI.Unshipped.txt index e4f699a4..da3ef19f 100644 --- a/Source/Core/PublicAPI.Unshipped.txt +++ b/Source/Core/PublicAPI.Unshipped.txt @@ -128,3 +128,4 @@ Open.Database.Extensions.MappedQueryWithTake.WithOptions(Open.Database.Extens Open.Database.Extensions.MappedCommand.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedCommand Open.Database.Extensions.MappedDbQuery.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedDbQuery! Open.Database.Extensions.MappedDbQuery.WithOptions(Open.Database.Extensions.QueryOptions options) -> Open.Database.Extensions.MappedDbQuery! +override Open.Database.Extensions.ExpressiveCommand.GetProcedureParameterNamesAsync(System.Data.IDbConnection! connection, string! procedure, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask> diff --git a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs index c546b68d..9abd6c6e 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs @@ -17,6 +17,7 @@ sealed class SchemaConnection(DataTable? table, Exception? failure) : DbConnecti { public DerivedParameterNameTests.RecordingDbCommand Command { get; } = new(); public int SchemaCalls { get; private set; } + public int AsyncSchemaCalls { get; private set; } public string? Collection { get; private set; } public string?[]? Restrictions { get; private set; } @@ -40,6 +41,19 @@ public override DataTable GetSchema(string collectionName, string?[] restriction if (failure is not null) throw failure; return table ?? throw new NotSupportedException(); } + + // The base implementation calls GetSchema, so SchemaCalls counts both. + public override Task GetSchemaAsync( + string collectionName, + string?[] restrictionValues, + CancellationToken cancellationToken) + { + AsyncSchemaCalls++; + return base.GetSchemaAsync( + collectionName, + restrictionValues, + cancellationToken); + } } // The columns of INFORMATION_SCHEMA.PARAMETERS that matter: a row per parameter, ordinal 0 is the return value. @@ -318,6 +332,39 @@ public static void ExpressiveCommand_OverADbConnection_LooksTheNamesUpToo() Assert.Equal(1, connection.SchemaCalls); } + [Fact] + public static async Task ExpressiveCommand_OverADbConnection_LooksTheNamesUpAsynchronously_OnTheAsyncPath() + { + const int orderId = 5; + var connection = new SchemaConnection(Table(Row(1, "@order_id")), null); + IDbConnection asInterface = connection; + + connection.Command.Rows = new DataTable(); + + await new ExpressiveCommand(asInterface, CommandType.StoredProcedure, Proc()) + .AddParam(orderId) + .ExecuteReaderAsync( + static _ => { }, + TestContext.Current.CancellationToken); + + Assert.Equal(["@order_id"], Sent(connection)); + Assert.Equal(1, connection.AsyncSchemaCalls); + } + + [Fact] + public static void ExpressiveCommand_OverADbConnection_LooksTheNamesUpSynchronously_OnTheSyncPath() + { + const int orderId = 5; + var connection = new SchemaConnection(Table(Row(1, "@order_id")), null); + IDbConnection asInterface = connection; + + new ExpressiveCommand(asInterface, CommandType.StoredProcedure, Proc()).AddParam(orderId).ExecuteNonQuery(); + + Assert.Equal(["@order_id"], Sent(connection)); + Assert.Equal(1, connection.SchemaCalls); + Assert.Equal(0, connection.AsyncSchemaCalls); + } + [Fact] public static void ExpressiveCommand_OverAConnectionThatIsNotADbConnection_UsesTheNameAsWritten() { From 176a2e3aebf8918ebb33fac46a5e5280b514c3c2 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Wed, 2 Dec 2026 14:32:59 -0800 Subject: [PATCH 2/3] Review: say which targets look the names up asynchronously, and test more The override's summary now says the lookup is asynchronous on net10.0 and later (GetSchemaAsync) and synchronous on the netstandard targets. Its header and inheritdoc are wrapped to about 80 columns. Tests: the synchronous run is written one call per line; an asynchronous lookup over a connection that is not a DbConnection gives no names and touches nothing; the caller's cancellation token reaches GetSchemaAsync. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/ExpressiveCommand.cs | 15 ++++-- .../ProcedureSchemaNamesTests.cs | 52 ++++++++++++++++++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Source/Core/ExpressiveCommand.cs b/Source/Core/ExpressiveCommand.cs index fd45f4e8..b86599c4 100644 --- a/Source/Core/ExpressiveCommand.cs +++ b/Source/Core/ExpressiveCommand.cs @@ -128,14 +128,21 @@ protected override ImmutableArray GetProcedureParameterNames(IDbConnecti /// /// Asynchronously gets the parameter names a stored procedure declares /// through the ADO.NET schema collection ProcedureParameters when the - /// connection is a , so the lookup does not block - /// the calling thread. + /// connection is a . The lookup is asynchronous + /// on net10.0 and later (GetSchemaAsync) and synchronous on the + /// netstandard targets, which have no such method. /// /// An open connection to run the lookup on. /// The procedure name as given to the command. /// A token that can cancel the lookup. - /// - protected override ValueTask> GetProcedureParameterNamesAsync(IDbConnection connection, string procedure, CancellationToken cancellationToken) + /// + protected override ValueTask> + GetProcedureParameterNamesAsync( + IDbConnection connection, + string procedure, + CancellationToken cancellationToken) => connection is DbConnection db ? ProcedureSchemaNames.GetAsync(db, procedure, cancellationToken) : default; diff --git a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs index 9abd6c6e..7e18faa8 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs @@ -18,6 +18,7 @@ sealed class SchemaConnection(DataTable? table, Exception? failure) : DbConnecti public DerivedParameterNameTests.RecordingDbCommand Command { get; } = new(); public int SchemaCalls { get; private set; } public int AsyncSchemaCalls { get; private set; } + public CancellationToken SchemaToken { get; private set; } public string? Collection { get; private set; } public string?[]? Restrictions { get; private set; } @@ -49,6 +50,7 @@ public override Task GetSchemaAsync( CancellationToken cancellationToken) { AsyncSchemaCalls++; + SchemaToken = cancellationToken; return base.GetSchemaAsync( collectionName, restrictionValues, @@ -358,13 +360,61 @@ public static void ExpressiveCommand_OverADbConnection_LooksTheNamesUpSynchronou var connection = new SchemaConnection(Table(Row(1, "@order_id")), null); IDbConnection asInterface = connection; - new ExpressiveCommand(asInterface, CommandType.StoredProcedure, Proc()).AddParam(orderId).ExecuteNonQuery(); + new ExpressiveCommand(asInterface, CommandType.StoredProcedure, Proc()) + .AddParam(orderId) + .ExecuteNonQuery(); Assert.Equal(["@order_id"], Sent(connection)); Assert.Equal(1, connection.SchemaCalls); Assert.Equal(0, connection.AsyncSchemaCalls); } + // Exposes the protected lookup, to call it without a run around it. + sealed class LookupCommand() + : ExpressiveCommand( + new ConnectionProbe(ConnectionState.Open).Connection, + CommandType.StoredProcedure, + "dbo.p") + { + public ValueTask> NamesAsync( + IDbConnection connection, + string procedure, + CancellationToken cancellationToken) + => GetProcedureParameterNamesAsync( + connection, + procedure, + cancellationToken); + } + + [Fact] + public static async Task ExpressiveCommand_AsyncLookup_OverAConnectionThatIsNotADbConnection_GivesNoNames() + { + var probe = new ConnectionProbe(ConnectionState.Open); + + ImmutableArray names = await new LookupCommand().NamesAsync( + probe.Connection, + Proc(), + TestContext.Current.CancellationToken); + + Assert.True(names.IsDefault); + Assert.Empty(probe.Calls); + } + + [Fact] + public static async Task ExpressiveCommand_AsyncLookup_PassesTheCallersTokenToGetSchemaAsync() + { + var connection = new SchemaConnection(Table(Row(1, "@order_id")), null); + using var source = new CancellationTokenSource(); + + ImmutableArray names = await new LookupCommand().NamesAsync( + connection, + Proc(), + source.Token); + + Assert.Equal(["@order_id"], names); + Assert.Equal(source.Token, connection.SchemaToken); + } + [Fact] public static void ExpressiveCommand_OverAConnectionThatIsNotADbConnection_UsesTheNameAsWritten() { From 6ca9762b4b7cbf7462f0c143cf109b13ee1021f3 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Wed, 2 Dec 2026 14:57:22 -0800 Subject: [PATCH 3/3] Docs: the token and the lookup on the netstandard targets, stated plainly The cancellation token of the asynchronous names lookup can cancel it only on net10.0 and later; on the netstandard targets it is checked before the synchronous lookup starts, and the call blocks the thread and returns a completed task. Say so in the override's documentation, and document the OperationCanceledException. A test comment no longer claims what the default GetSchemaAsync does. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/ExpressiveCommand.cs | 14 +++++++++++--- .../ProcedureSchemaNamesTests.cs | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Source/Core/ExpressiveCommand.cs b/Source/Core/ExpressiveCommand.cs index b86599c4..b0ef851d 100644 --- a/Source/Core/ExpressiveCommand.cs +++ b/Source/Core/ExpressiveCommand.cs @@ -129,12 +129,20 @@ protected override ImmutableArray GetProcedureParameterNames(IDbConnecti /// Asynchronously gets the parameter names a stored procedure declares /// through the ADO.NET schema collection ProcedureParameters when the /// connection is a . The lookup is asynchronous - /// on net10.0 and later (GetSchemaAsync) and synchronous on the - /// netstandard targets, which have no such method. + /// on net10.0 and later (GetSchemaAsync). The netstandard targets have + /// no such method: there the lookup blocks the calling thread and returns a + /// task that is already complete. /// /// An open connection to run the lookup on. /// The procedure name as given to the command. - /// A token that can cancel the lookup. + /// + /// On net10.0 and later the token is passed to GetSchemaAsync and can + /// cancel the lookup; on the netstandard targets it is only checked before + /// the lookup starts. + /// + /// + /// is canceled. + /// /// diff --git a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs index 7e18faa8..f1f51121 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ProcedureSchemaNamesTests.cs @@ -43,7 +43,7 @@ public override DataTable GetSchema(string collectionName, string?[] restriction return table ?? throw new NotSupportedException(); } - // The base implementation calls GetSchema, so SchemaCalls counts both. + // The default GetSchemaAsync may call GetSchema, so SchemaCalls can count both. public override Task GetSchemaAsync( string collectionName, string?[] restrictionValues,