Skip to content

Commit 734fa77

Browse files
committed
Change existing async helper calls in SqlCommand
1 parent 80beeff commit 734fa77

4 files changed

Lines changed: 36 additions & 40 deletions

File tree

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Encryption.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,22 @@ private SqlDataReader GetParameterEncryptionDataReader(
253253
bool isRetry)
254254
{
255255
returnTask = AsyncHelper.CreateContinuationTaskWithState(
256-
task: fetchInputParameterEncryptionInfoTask,
256+
taskToContinue: fetchInputParameterEncryptionInfoTask,
257257
state: this,
258-
onSuccess: state =>
258+
onSuccess: sqlCommand =>
259259
{
260-
SqlCommand command = (SqlCommand)state;
261260
bool processFinallyBlockAsync = true;
262261
bool decrementAsyncCountInFinallyBlockAsync = true;
263262

264263
try
265264
{
266265
// Check for any exceptions on network write, before reading.
267-
command.CheckThrowSNIException();
266+
sqlCommand.CheckThrowSNIException();
268267

269268
// If it is async, then TryFetchInputParameterEncryptionInfo ->
270269
// RunExecuteReaderTds would have incremented the async count. Decrement it
271270
// when we are about to complete async execute reader.
272-
SqlConnectionInternal internalConnectionTds = command._activeConnection.GetOpenTdsConnection();
271+
SqlConnectionInternal internalConnectionTds = sqlCommand._activeConnection.GetOpenTdsConnection();
273272
if (internalConnectionTds is not null)
274273
{
275274
internalConnectionTds.DecrementAsyncCount();
@@ -278,13 +277,13 @@ private SqlDataReader GetParameterEncryptionDataReader(
278277

279278
// Complete executereader.
280279
// @TODO: If we can remove this reference, this could be a static lambda
281-
describeParameterEncryptionDataReader = command.CompleteAsyncExecuteReader(
280+
describeParameterEncryptionDataReader = sqlCommand.CompleteAsyncExecuteReader(
282281
isInternal: false,
283282
forDescribeParameterEncryption: true);
284-
Debug.Assert(command._stateObj is null, "non-null state object in PrepareForTransparentEncryption.");
283+
Debug.Assert(sqlCommand._stateObj is null, "non-null state object in PrepareForTransparentEncryption.");
285284

286285
// Read the results of describe parameter encryption.
287-
command.ReadDescribeEncryptionParameterResults(
286+
sqlCommand.ReadDescribeEncryptionParameterResults(
288287
describeParameterEncryptionDataReader,
289288
describeParameterEncryptionRpcOriginalRpcMap,
290289
isRetry);
@@ -304,7 +303,7 @@ private SqlDataReader GetParameterEncryptionDataReader(
304303
}
305304
finally
306305
{
307-
command.PrepareTransparentEncryptionFinallyBlock(
306+
sqlCommand.PrepareTransparentEncryptionFinallyBlock(
308307
closeDataReader: processFinallyBlockAsync,
309308
decrementAsyncCount: decrementAsyncCountInFinallyBlockAsync,
310309
clearDataStructures: processFinallyBlockAsync,
@@ -313,11 +312,9 @@ private SqlDataReader GetParameterEncryptionDataReader(
313312
describeParameterEncryptionDataReader: describeParameterEncryptionDataReader);
314313
}
315314
},
316-
onFailure: static (exception, state) =>
315+
onFailure: static (sqlCommand, exception) =>
317316
{
318-
SqlCommand command = (SqlCommand)state;
319-
command.CachedAsyncState?.ResetAsyncState();
320-
317+
sqlCommand.CachedAsyncState?.ResetAsyncState();
321318
if (exception is not null)
322319
{
323320
throw exception;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ private IAsyncResult BeginExecuteNonQueryInternal(
223223
if (execNonQuery is not null)
224224
{
225225
AsyncHelper.ContinueTaskWithState(
226-
task: execNonQuery,
227-
completion: localCompletion,
228-
state: Tuple.Create(this, localCompletion),
229-
onSuccess: static state =>
226+
taskToContinue: execNonQuery,
227+
taskCompletionSource: localCompletion,
228+
state1: this,
229+
state2: localCompletion,
230+
onSuccess: static (sqlCommand, localCompletion) =>
230231
{
231-
var parameters = (Tuple<SqlCommand, TaskCompletionSource<object>>)state;
232-
parameters.Item1.BeginExecuteNonQueryInternalReadStage(parameters.Item2);
232+
sqlCommand.BeginExecuteNonQueryInternalReadStage(localCompletion);
233233
});
234234
}
235235
else

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,18 +1599,18 @@ private Task RunExecuteReaderTdsSetupContinuation(
15991599
{
16001600
// @TODO: Why use the state version if we can't make this a static helper?
16011601
return AsyncHelper.CreateContinuationTaskWithState(
1602-
task: writeTask,
1602+
taskToContinue: writeTask,
16031603
state: _activeConnection,
1604-
onSuccess: state =>
1604+
onSuccess: sqlConnection =>
16051605
{
16061606
// This will throw if the connection is closed.
16071607
// @TODO: So... can we have something that specifically does that?
1608-
((SqlConnection)state).GetOpenTdsConnection();
1608+
sqlConnection.GetOpenTdsConnection();
16091609
CachedAsyncState.SetAsyncReaderState(ds, runBehavior, optionSettings);
16101610
},
1611-
onFailure: static (exception, state) =>
1611+
onFailure: static (sqlConnection, _) =>
16121612
{
1613-
((SqlConnection)state).GetOpenTdsConnection().DecrementAsyncCount();
1613+
sqlConnection.GetOpenTdsConnection().DecrementAsyncCount();
16141614
});
16151615
}
16161616

@@ -1632,7 +1632,7 @@ private void RunExecuteReaderTdsSetupReconnectContinuation(
16321632
AsyncHelper.SetTimeoutException(
16331633
completion,
16341634
timeout,
1635-
onFailure: static () => SQL.CR_ReconnectTimeout(),
1635+
onTimeout: static () => SQL.CR_ReconnectTimeout(),
16361636
timeoutCts.Token);
16371637

16381638
// @TODO: With an object to pass around we can use the state-based version
@@ -1703,14 +1703,13 @@ private SqlDataReader RunExecuteReaderTdsWithTransparentParameterEncryption(
17031703
// @TODO: This is a prime candidate for proper async-await execution
17041704
TaskCompletionSource<object> completion = new TaskCompletionSource<object>();
17051705
AsyncHelper.ContinueTaskWithState(
1706-
task: describeParameterEncryptionTask,
1707-
completion: completion,
1706+
taskToContinue: describeParameterEncryptionTask,
1707+
taskCompletionSource: completion,
17081708
state: this,
1709-
onSuccess: state =>
1709+
onSuccess: sqlCommand =>
17101710
{
1711-
SqlCommand command = (SqlCommand)state;
1712-
command.GenerateEnclavePackage();
1713-
command.RunExecuteReaderTds(
1711+
sqlCommand.GenerateEnclavePackage();
1712+
sqlCommand.RunExecuteReaderTds(
17141713
cmdBehavior,
17151714
runBehavior,
17161715
returnStream,
@@ -1729,23 +1728,23 @@ private SqlDataReader RunExecuteReaderTdsWithTransparentParameterEncryption(
17291728
else
17301729
{
17311730
AsyncHelper.ContinueTaskWithState(
1732-
task: subTask,
1733-
completion: completion,
1731+
taskToContinue: subTask,
1732+
taskCompletionSource: completion,
17341733
state: completion,
1735-
onSuccess: static state => ((TaskCompletionSource<object>)state).SetResult(null));
1734+
onSuccess: static state => state.SetResult(null));
17361735
}
17371736
},
1738-
onFailure: static (exception, state) =>
1737+
onFailure: static (sqlCommand, exception) =>
17391738
{
1740-
((SqlCommand)state).CachedAsyncState?.ResetAsyncState();
1739+
sqlCommand.CachedAsyncState?.ResetAsyncState();
17411740
if (exception is not null)
17421741
{
17431742
throw exception;
17441743
}
17451744
},
1746-
onCancellation: static state =>
1745+
onCancellation: static sqlCommand =>
17471746
{
1748-
((SqlCommand)state).CachedAsyncState?.ResetAsyncState();
1747+
sqlCommand.CachedAsyncState?.ResetAsyncState();
17491748
});
17501749

17511750
task = completion.Task;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ private IAsyncResult BeginExecuteXmlReaderInternal(
262262
if (writeTask is not null)
263263
{
264264
AsyncHelper.ContinueTaskWithState(
265-
task: writeTask,
266-
completion: localCompletion,
265+
taskToContinue: writeTask,
266+
taskCompletionSource: localCompletion,
267267
state: Tuple.Create(this, localCompletion),
268268
onSuccess: static state =>
269269
{

0 commit comments

Comments
 (0)