示例#1
0
        static ScopeProvider()
        {
            SafeCallContext.Register(
                () =>
            {
                var scope   = GetCallContextObject <Scope>(ScopeItemKey);
                var context = GetCallContextObject <ScopeContext>(ContextItemKey);
                SetCallContextObject(ScopeItemKey, null);
                SetCallContextObject(ContextItemKey, null);
                return(Tuple.Create(scope, context));
            },
                o =>
            {
                // cannot re-attached over leaked scope/context
                if (GetCallContextObject <Scope>(ScopeItemKey) != null)
                {
                    throw new Exception("Found leaked scope when restoring call context.");
                }
                if (GetCallContextObject <ScopeContext>(ContextItemKey) != null)
                {
                    throw new Exception("Found leaked context when restoring call context.");
                }

                var t = (Tuple <Scope, ScopeContext>)o;
                SetCallContextObject(ScopeItemKey, t.Item1);
                SetCallContextObject(ContextItemKey, t.Item2);
            });
        }
 static CallContextTests()
 {
     SafeCallContext.Register(() =>
     {
         CallContext.FreeNamedDataSlot("test1");
         CallContext.FreeNamedDataSlot("test2");
         return(null);
     }, o => {});
 }
示例#3
0
        protected HybridAccessorBase(IHttpContextAccessor httpContextAccessor)
        {
            if (httpContextAccessor == null)
            {
                throw new ArgumentNullException(nameof(httpContextAccessor));
            }
            _httpContextAccessor = httpContextAccessor;

            lock (Locker)
            {
                // register the itemKey once with SafeCallContext
                if (_registered)
                {
                    return;
                }
                _registered = true;
            }

            // ReSharper disable once VirtualMemberCallInConstructor
            var itemKey = ItemKey; // virtual

            SafeCallContext.Register(() =>
            {
                var value = CallContext.LogicalGetData(itemKey);
                CallContext.FreeNamedDataSlot(itemKey);
                return(value);
            }, o =>
            {
                if (o == null)
                {
                    return;
                }
                var value = o as T;
                if (value == null)
                {
                    throw new ArgumentException($"Expected type {typeof(T).FullName}, got {o.GetType().FullName}", nameof(o));
                }
                CallContext.LogicalSetData(itemKey, value);
            });
        }
示例#4
0
        static ScopeProvider()
        {
            SafeCallContext.Register(
                () =>
            {
                var scope   = GetCallContextObject <IScopeInternal>(ScopeItemKey);
                var context = GetCallContextObject <ScopeContext>(ContextItemKey);
                SetCallContextObject(ScopeItemKey, null);
                SetCallContextObject(ContextItemKey, null);
                return(Tuple.Create(scope, context));
            },
                o =>
            {
                // cannot re-attached over leaked scope/context
                // except of course over NoScope (which leaks)
                var ambientScope = GetCallContextObject <IScope>(ScopeItemKey);
                if (ambientScope != null)
                {
                    var ambientNoScope = ambientScope as NoScope;
                    if (ambientNoScope == null)
                    {
                        throw new Exception("Found leaked scope when restoring call context.");
                    }

                    // this should rollback any pending transaction
                    ambientNoScope.Dispose();
                }
                if (GetCallContextObject <ScopeContext>(ContextItemKey) != null)
                {
                    throw new Exception("Found leaked context when restoring call context.");
                }

                var t = (Tuple <IScopeInternal, ScopeContext>)o;
                SetCallContextObject(ScopeItemKey, t.Item1);
                SetCallContextObject(ContextItemKey, t.Item2);
            });
        }