public AmbientContextSuppressor() { _savedScope = DbContextScope.GetAmbientScope(); // We're hiding the ambient scope but not removing its instance // altogether. This is to be tolerant to some programming errors. // // Suppose we removed the ambient scope instance here. If someone // was to start a parallel task without suppressing // the ambient context and then tried to suppress the ambient // context within the parallel task while the original flow // of execution was still ongoing (a strange thing to do, I know, // but I'm sure this is going to happen), we would end up // removing the ambient context instance of the original flow // of execution from within the parallel flow of execution! // // As a result, any code in the original flow of execution // that would attempt to access the ambient scope would end up // with a null value since we removed the instance. // // It would be a fairly nasty bug to track down. So don't let // that happen. Hiding the ambient scope (i.e. clearing the CallContext // in our execution flow but leaving the ambient scope instance untouched) // is safe. DbContextScope.HideAmbientScope(); }
private static DbContextScope GetConfirmScope() { var scope = DbContextScope.GetAmbientScope(); if (scope == null) { throw new InvalidOperationException("No open DB Scope detected, did you forget to open one?"); } return(scope); }
public void Dispose() { if (_disposed) return; if (_savedScope != null) { DbContextScope.SetAmbientScope(_savedScope); _savedScope = null; } _disposed = true; }
public DbContextScope(ILocalCache cache, Func <ApiContext> apiContextCreator, ISettingsStorage settingsStorage) { _parentScope = GetAmbientScope(); if (_parentScope == null) { _contexts = new DbContexts(cache, apiContextCreator, settingsStorage); } else { _contexts = _parentScope._contexts; _nested = true; } SetAmbientScope(this); }
public void Dispose() { if (_disposed) { return; } if (_savedScope != null) { DbContextScope.SetAmbientScope(_savedScope); _savedScope = null; } _disposed = true; }
/// <summary> /// Makes the provided 'dbContextScope' available as the the ambient scope via the CallContext. /// </summary> internal static void SetAmbientScope(DbContextScope newAmbientScope) { if (newAmbientScope == null) { throw new ArgumentNullException(nameof(newAmbientScope)); } var current = GetIdentifier(); var id = newAmbientScope._instanceIdentifier; if (current == id) { return; } // Store the new scope's instance identifier in the CallContext, making it the ambient scope SetId(id); // Keep track of this instance (or do nothing if we're already tracking it) dbContextScopeInstances.GetValue(id, key => newAmbientScope); }
/// <summary> /// Makes the provided 'dbContextScope' available as the the ambient scope via the CallContext. /// </summary> internal static void SetAmbientScope(DbContextScope newAmbientScope) { if (newAmbientScope == null) throw new ArgumentNullException(nameof(newAmbientScope)); var current = GetIdentifier(); var id = newAmbientScope._instanceIdentifier; if (current == id) return; // Store the new scope's instance identifier in the CallContext, making it the ambient scope SetId(id); // Keep track of this instance (or do nothing if we're already tracking it) dbContextScopeInstances.GetValue(id, key => newAmbientScope); }
public DbContextScope(ILocalCache cache, Func<ApiContext> apiContextCreator, ISettingsStorage settingsStorage) { _parentScope = GetAmbientScope(); if (_parentScope == null) _contexts = new DbContexts(cache, apiContextCreator, settingsStorage); else { _contexts = _parentScope._contexts; _nested = true; } SetAmbientScope(this); }