private ExecutionContextStorage GetCurrentContextInternal() { if (IsDisposed) { return(ExecutionContextStorage.GetEmptyContext()); } return(_noFlowContext.Value ?? _executionContextStore.Value); }
public Guid SetAndGetContextElement(Guid contextTypeGuid, Guid contextElement) { if (IsDisposed) { return(Guid.Empty); } var storage1 = GetCurrentContextInternal() ?? ExecutionContextStorage.GetEmptyContext(); var storage2 = storage1.UpdateElement(contextTypeGuid, contextElement, out var previeousValue); _executionContextStore.Value = storage2.IsEmpty ? null : storage2; _noFlowContext.Value = null; return(previeousValue); }
public uint GetCurrentContext() { var newContext = GetCurrentContextInternal(); if (newContext == null || newContext.IsEmpty) { return(0); } if (newContext.IsNoFlowContext) { newContext = new ExecutionContextStorage(newContext.PreviousContext, newContext); } return(_contextCookies.Insert(newContext)); }
private void RaiseNotificationForContextChange(ExecutionContextStorage oldContext, ExecutionContextStorage newContext) { var listeners = _listeners; if (listeners == null || listeners.Count == 0) { return; } lock (listeners) { foreach (var keyValuePair in listeners) { var oldValue = oldContext?.GetElement(keyValuePair.Key) ?? Guid.Empty; var newValue = newContext?.GetElement(keyValuePair.Key) ?? Guid.Empty; if (oldValue != newValue) { if (!_pendingNotifications.IsValueCreated) { _pendingNotifications.Value = new List <PendingNotification>(); } foreach (var listener in keyValuePair.Value) { _pendingNotifications.Value.Add(new PendingNotification(keyValuePair.Key, oldValue, newValue, listener)); } } } } if (!_pendingNotifications.IsValueCreated) { return; } foreach (var pendingNotification in _pendingNotifications.Value) { pendingNotification.Notify(); } _pendingNotifications.Value.Clear(); }
public void PushContextEx(uint cookie, bool dontTrackAsyncWork) { if (cookie == 0 || IsDisposed) { return; } if (!_contextCookies.TryGetValue(cookie, out var emptyContext)) { return; } var currentContext = GetCurrentContextInternal(); var newContext = new ExecutionContextStorage(currentContext, emptyContext); if (!dontTrackAsyncWork) { _executionContextStore.Value = newContext; } else { RaiseNotificationForContextChange(currentContext, newContext); newContext.IsNoFlowContext = true; _noFlowContext.Value = newContext; } }
public ExecutionContextStorage(ExecutionContextStorage previousContext, ExecutionContextStorage newContext) { _elements = newContext._elements; PreviousContext = previousContext; }
private ExecutionContextStorage(ExecutionContextStorage previousContext, List <Tuple <Guid, Guid> > elements) { PreviousContext = previousContext; _elements = elements; }