public async Task <bool> TryAllocate(string key, Func <Task <IDocument> > doc, Func <Task <Stream> > stream) { if (AllocatedFiles.Count >= MaxAllocatedFiles && CacheInvalidator != null) { int filesToDeallocate = 1 + (AllocatedFiles.Count - MaxAllocatedFiles); IEnumerable <Task> deallocateAwait = CacheInvalidator.DeallocatePriorityFiles.Result .Take(filesToDeallocate) .SelectMany(x => OnDeallocate.Select(y => y(x))); await Task.WhenAll(deallocateAwait); } if (!AllocatedFiles.Contains(key)) { AllocatedFiles.Add(key); await CacheInvalidator.Allocated(key, doc, stream); return(true); } else { return(false); } }