public void Dispose()
            {
                var scope = (IServiceScope)_threadData[ThreadDataKey];

                scope?.Dispose();
                _threadData.SetValue(ThreadDataKey, null);
            }
        private Dictionary <Type, DataInterceptor> GetDataInterceptors(ThreadDataManagerData threadData)
        {
            Verify.ArgumentNotNull(threadData, nameof(threadData));
            const string threadDataKey = "DataFacade:DataInterceptors";

            var dataInterceptors = threadData.GetValue(threadDataKey) as Dictionary <Type, DataInterceptor>;

            if (dataInterceptors == null)
            {
                dataInterceptors = new Dictionary <Type, DataInterceptor>();
                threadData.SetValue(threadDataKey, dataInterceptors);
            }

            return(dataInterceptors);
        }
        // private static readonly IEnumerable<Measurement> EmptyReport = new Measurement[0];


        /// <exclude />
        public static void BeginProfiling()
        {
            if (Disabled)
            {
                return;
            }

            ThreadDataManagerData threadData = ThreadDataManager.Current;

            Verify.That(!threadData.HasValue(ProfilerKey), "Profiler has already been initialized");

            var stack = new Stack <Measurement>();

            stack.Push(new Measurement("Root")
            {
                MemoryUsage = GC.GetTotalMemory(true)
            });
            threadData.SetValue(ProfilerKey, stack);
        }
        /// <exclude />
        public static IDisposable Measure(string name, Func <EntityToken> entityTokenFactory)
        {
            if (Disabled)
            {
                return(EmptyDisposable.Instance);
            }

            Measurement         currentNode;
            Stack <Measurement> stack;
            bool isInParallel;

            if (!GetCurrentNode(out currentNode, out stack, out isInParallel))
            {
                return(EmptyDisposable.Instance);
            }

            Stack <Measurement> newNodeStack;

            if (isInParallel)
            {
                ThreadDataManagerData currentThreadData = ThreadDataManager.Current;

                if (currentThreadData.HasValue(currentThreadData))
                {
                    newNodeStack = currentThreadData[ProfilerKey] as Stack <Measurement>;
                }
                else
                {
                    newNodeStack = new Stack <Measurement>();
                    currentThreadData.SetValue(ProfilerKey, newNodeStack);
                }
            }
            else
            {
                newNodeStack = stack;
            }

            return(new InfoCollector(currentNode, name, isInParallel, newNodeStack, entityTokenFactory));
        }