/// <summary> /// Clears the ambient scope from the CallContext and stops tracking its instance. /// Call this when a DbContextScope is being disposed. /// </summary> internal static void RemoveAmbientScope() { #if NET45 || NET462 var current = CallContext.LogicalGetData(AmbientDbContextScopeKey) as InstanceIdentifier; CallContext.LogicalSetData(AmbientDbContextScopeKey, null); #elif NETSTANDARD2 var current = CallContext.GetData(AmbientDbContextScopeKey) as InstanceIdentifier; CallContext.SetData(AmbientDbContextScopeKey, null); #endif // If there was an ambient scope, we can stop tracking it now if (current != null) { DbContextScopeInstances.Remove(current); } }
/// <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("newAmbientScope"); } var current = CallContext.LogicalGetData(AmbientDbContextScopeKey) as InstanceIdentifier; if (current == newAmbientScope._instanceIdentifier) { return; } // Store the new scope's instance identifier in the CallContext, making it the ambient scope CallContext.LogicalSetData(AmbientDbContextScopeKey, newAmbientScope._instanceIdentifier); // Keep track of this instance (or do nothing if we're already tracking it) DbContextScopeInstances.GetValue(newAmbientScope._instanceIdentifier, key => newAmbientScope); }
/// <summary> /// Clears the ambient scope from the CallContext but keeps tracking its instance. Call this to temporarily /// hide the ambient context (e.g. to prevent it from being captured by parallel task). /// </summary> internal static void HideAmbientScope() { CallContext.LogicalSetData(AmbientDbContextScopeKey, null); }