Exemplo n.º 1
0
 private Lazy <Type> CreateAssembledType(AssembledTypeID typeID)
 {
     return(new Lazy <Type> (
                () =>
     {
         var assemblyContext = _assemblyContextPool.Dequeue();
         try
         {
             var result = _typeAssembler.AssembleType(typeID, assemblyContext.ParticipantState, assemblyContext.MutableTypeBatchCodeGenerator);
             AddAdditionalTypesToCache(result.AdditionalTypes);
             return result.Type;
         }
         finally
         {
             _assemblyContextPool.Enqueue(assemblyContext);
         }
     },
                LazyThreadSafetyMode.ExecutionAndPublication));
 }
        public AssemblyContext Dequeue()
        {
            var data = CallContext.GetData(_instanceID);

            if (data is HashSet <AssemblyContext> )
            {
                throw new InvalidOperationException(
                          "Dequeue() cannot be invoked from the same thread as DequeueAll() until all dequeued AssemblyContext have been returned to the pool.");
            }

            var threadLocalAssemblyContext = data as ThreadLocalAssemblyContext;

            if (threadLocalAssemblyContext == null)
            {
                var assemblyContext = _assemblyContextPool.Dequeue();
                threadLocalAssemblyContext = new ThreadLocalAssemblyContext(assemblyContext);
                CallContext.SetData(_instanceID, threadLocalAssemblyContext);
            }

            threadLocalAssemblyContext.IncrementCount();
            return(threadLocalAssemblyContext.Value);
        }