示例#1
0
 private ExecutionContextStorage GetCurrentContextInternal()
 {
     if (IsDisposed)
     {
         return(ExecutionContextStorage.GetEmptyContext());
     }
     return(_noFlowContext.Value ?? _executionContextStore.Value);
 }
示例#2
0
        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);
        }
示例#3
0
        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));
        }
示例#4
0
        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();
        }
示例#5
0
        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;
            }
        }
示例#6
0
 public ExecutionContextStorage(ExecutionContextStorage previousContext, ExecutionContextStorage newContext)
 {
     _elements       = newContext._elements;
     PreviousContext = previousContext;
 }
示例#7
0
 private ExecutionContextStorage(ExecutionContextStorage previousContext, List <Tuple <Guid, Guid> > elements)
 {
     PreviousContext = previousContext;
     _elements       = elements;
 }