public CacheResult BuildPackage(DateTime packageDate) { List <byte[]> streamBytesList = new List <byte[]>(); //create file and store in cache if file does not exist in cache var keys = _temporaryExposureKeyRepository.GetKeysOnlyFromApiOriginCountry(packageDate, _fetchCommandTimeout); int skip = 0; while (keys.Count > skip) { var keysBatch = keys.Skip(skip).Take(_maxKeysPerFile); skip += _maxKeysPerFile; using (Stream stream = _databaseKeysToBinaryStreamMapperService.ExportDiagnosisKeys(keysBatch.ToList())) { using (var memoryStream = new MemoryStream()) { stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(memoryStream); stream.Seek(0, SeekOrigin.Begin); streamBytesList.Add(memoryStream.ToArray()); } } } return(new CacheResult() { FileBytesList = streamBytesList }); }