internal static void UpsertEntireSessionDocument( this MongoSessionStateStore obj, IMongoCollection <BsonDocument> sessionCollection, BsonDocument insertDoc) { int attempts = 0; while (true) { try { var task = sessionCollection.InsertOneAsync(insertDoc); task.Wait(); if (task.IsFaulted) { throw new Exception("retry!"); } return; } catch (Exception e) { PauseOrThrow(ref attempts, obj, sessionCollection, e); } } }
private static void PauseOrThrow( ref int attempts, MongoSessionStateStore obj, IMongoCollection <BsonDocument> sessionCollection, Exception e) { if (attempts < obj.MaxUpsertAttempts) { attempts++; System.Threading.Thread.CurrentThread.Join(obj.MsWaitingForAttempt); } else { throw new ProviderException(MongoSessionStateStore.EXCEPTION_MESSAGE); } }
internal static BsonDocument FindOneSessionItem( this MongoSessionStateStore obj, IMongoCollection <BsonDocument> sessionCollection, FilterDefinition <BsonDocument> q) { int nAtempts = 0; while (true) { try { var task = sessionCollection.Find(q).FirstOrDefaultAsync(); task.Wait(); return(task.Result); } catch (Exception e) { PauseOrThrow(ref nAtempts, obj, sessionCollection, e); } } }
/// <summary> /// NOT used. It's preserved for future implementations. /// </summary> /// <param name="obj"></param> /// <param name="e"></param> /// <param name="action"></param> /// <param name="eventType"></param> internal static void WriteToEventLog( this MongoSessionStateStore obj, Exception e, string action, EventLogEntryType eventType = EventLogEntryType.Error) { if (obj.WriteExceptionsToEventLog) { using (var log = new EventLog()) { log.Source = MongoSessionStateStore.EVENT_SOURCE; log.Log = MongoSessionStateStore.EVENT_LOG; string message = String.Format("An exception occurred communicating with the data source.\n\nAction: {0}\n\nException: {1}", action, e); log.WriteEntry(message, eventType); } } }
internal static DeleteResult DeleteSessionDocument( this MongoSessionStateStore obj, IMongoCollection <BsonDocument> sessionCollection, FilterDefinition <BsonDocument> query) { int attempts = 0; while (true) { try { var task = sessionCollection.DeleteManyAsync(query); task.Wait(); return(task.Result); } catch (Exception e) { PauseOrThrow(ref attempts, obj, sessionCollection, e); } } }