@@ -6,7 +6,7 @@ namespace Microsoft.AspNet.SessionState
66 using Resources ;
77 using System ;
88 using System . Data . SqlClient ;
9- using System . Threading ;
9+ using System . Runtime . CompilerServices ;
1010 using System . Threading . Tasks ;
1111 using System . Web ;
1212 using System . Web . SessionState ;
@@ -47,10 +47,10 @@ Created datetime NOT NULL DEFAULT GETUTCDATE(),
4747 )
4848 CREATE NONCLUSTERED INDEX Index_Expires ON { SqlSessionStateRepositoryUtil . TableName } (Expires)
4949 END" ;
50- #endregion
50+ #endregion
5151
5252 #region TempInsertUninitializedItem
53- private static readonly string TempInsertUninitializedItemSql = $@ "
53+ private static readonly string TempInsertUninitializedItemSql = $@ "
5454 DECLARE @now AS datetime
5555 DECLARE @nowLocal AS datetime
5656 SET @now = GETUTCDATE()
@@ -75,10 +75,10 @@ DECLARE @nowLocal AS datetime
7575 @nowLocal,
7676 1,
7777 1)" ;
78- #endregion
78+ #endregion
7979
8080 #region GetStateItemExclusive
81- private static readonly string GetStateItemExclusiveSql = $@ "
81+ private static readonly string GetStateItemExclusiveSql = $@ "
8282 BEGIN TRAN
8383 DECLARE @textptr AS varbinary(16)
8484 DECLARE @length AS int
@@ -275,7 +275,7 @@ DEALLOCATE ExpiredSessionCursor
275275 END
276276
277277 DROP TABLE #tblExpiredSessions" ;
278- #endregion
278+ #endregion
279279 #endregion
280280
281281 public SqlSessionStateRepository ( string connectionString , int commandTimeout ,
@@ -310,17 +310,23 @@ internal int CommandTimeout
310310 }
311311 #endregion
312312
313- private bool CanRetry ( RetryCheckParameter parameter )
313+ private Task < bool > CanRetryAsync ( RetryCheckParameter parameter )
314314 {
315315 if ( _retryIntervalMilSec <= 0 ||
316316 ! SqlSessionStateRepositoryUtil . IsFatalSqlException ( parameter . Exception ) ||
317317 parameter . RetryCount >= _maxRetryNum )
318318 {
319- return false ;
319+ return Task . FromResult ( false ) ;
320320 }
321321
322+ return WaitToRetryAsync ( parameter , _retryIntervalMilSec ) ;
323+ }
324+
325+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
326+ private static async Task < bool > WaitToRetryAsync ( RetryCheckParameter parameter , int retryIntervalMilSec )
327+ {
322328 // sleep the specified time and allow retry
323- Thread . Sleep ( _retryIntervalMilSec ) ;
329+ await Task . Delay ( retryIntervalMilSec ) ;
324330 parameter . RetryCount ++ ;
325331
326332 return true ;
@@ -334,7 +340,7 @@ public void CreateSessionStateTable()
334340 try
335341 {
336342 var cmd = _commandHelper . CreateNewSessionTableCmd ( CreateSessionTableSql ) ;
337- var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) . ConfigureAwait ( false ) ;
343+ var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) . ConfigureAwait ( false ) ;
338344 task . GetAwaiter ( ) . GetResult ( ) ;
339345 }
340346 catch ( Exception ex )
@@ -349,7 +355,7 @@ public void DeleteExpiredSessions()
349355 using ( var connection = new SqlConnection ( _connectString ) )
350356 {
351357 var cmd = _commandHelper . CreateDeleteExpiredSessionsCmd ( DeleteExpiredSessionsSql ) ;
352- var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) . ConfigureAwait ( false ) ;
358+ var task = SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) . ConfigureAwait ( false ) ;
353359 task . GetAwaiter ( ) . GetResult ( ) ;
354360 }
355361 }
@@ -375,7 +381,7 @@ public async Task<SessionItem> GetSessionStateItemAsync(string id, bool exclusiv
375381
376382 using ( var connection = new SqlConnection ( _connectString ) )
377383 {
378- using ( var reader = await SqlSessionStateRepositoryUtil . SqlExecuteReaderWithRetryAsync ( connection , cmd , CanRetry ) )
384+ using ( var reader = await SqlSessionStateRepositoryUtil . SqlExecuteReaderWithRetryAsync ( connection , cmd , CanRetryAsync ) )
379385 {
380386 if ( await reader . ReadAsync ( ) )
381387 {
@@ -427,7 +433,7 @@ public async Task CreateOrUpdateSessionStateItemAsync(bool newItem, string id, b
427433
428434 using ( var connection = new SqlConnection ( _connectString ) )
429435 {
430- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry , newItem ) ;
436+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync , newItem ) ;
431437 }
432438 }
433439
@@ -436,7 +442,7 @@ public async Task ResetSessionItemTimeoutAsync(string id)
436442 var cmd = _commandHelper . CreateResetItemTimeoutCmd ( ResetItemTimeoutSql , id ) ;
437443 using ( var connection = new SqlConnection ( _connectString ) )
438444 {
439- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) ;
445+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) ;
440446 }
441447 }
442448
@@ -445,7 +451,7 @@ public async Task RemoveSessionItemAsync(string id, object lockId)
445451 var cmd = _commandHelper . CreateRemoveStateItemCmd ( RemoveStateItemSql , id , lockId ) ;
446452 using ( var connection = new SqlConnection ( _connectString ) )
447453 {
448- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) ;
454+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) ;
449455 }
450456 }
451457
@@ -454,7 +460,7 @@ public async Task ReleaseSessionItemAsync(string id, object lockId)
454460 var cmd = _commandHelper . CreateReleaseItemExclusiveCmd ( ReleaseItemExclusiveSql , id , lockId ) ;
455461 using ( var connection = new SqlConnection ( _connectString ) )
456462 {
457- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry ) ;
463+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync ) ;
458464 }
459465 }
460466
@@ -463,10 +469,10 @@ public async Task CreateUninitializedSessionItemAsync(string id, int length, byt
463469 var cmd = _commandHelper . CreateTempInsertUninitializedItemCmd ( TempInsertUninitializedItemSql , id , length , buf , timeout ) ;
464470 using ( var connection = new SqlConnection ( _connectString ) )
465471 {
466- await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetry , true ) ;
472+ await SqlSessionStateRepositoryUtil . SqlExecuteNonQueryWithRetryAsync ( connection , cmd , CanRetryAsync , true ) ;
467473 }
468474 }
469475 #endregion
470-
471- }
476+
477+ }
472478}
0 commit comments