diff --git a/Framework/Azure/Cqrs.Azure.Storage/BlobStorageStore.cs b/Framework/Azure/Cqrs.Azure.Storage/BlobStorageStore.cs index 895238ca2..ab4dcba7c 100644 --- a/Framework/Azure/Cqrs.Azure.Storage/BlobStorageStore.cs +++ b/Framework/Azure/Cqrs.Azure.Storage/BlobStorageStore.cs @@ -11,6 +11,7 @@ using System.IO; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using Azure; using Azure.Core; @@ -428,7 +429,7 @@ async Task> OpenStreamsForReadingAsync (Func predicate = null, string blobPrefix = null, string folderName = null) { IList results = null; - for(int i = 0; i < 3; i++) + for(int i = 0; i < 10; i++) { AsyncPageable blobs; if (!string.IsNullOrWhiteSpace(folderName)) @@ -438,13 +439,12 @@ async Task> OpenStreamsForReadingAsync var query = new Dictionary(); #if NET472 Task.Run(async () => -#endif { +#endif await foreach (BlobItem blob in blobs) query.Add(blob.Name, blob); - } #if NET472 - ).Wait(); + }).Wait(); #endif IEnumerable sourceQuery; @@ -458,37 +458,41 @@ async Task> OpenStreamsForReadingAsync IList downloadTasks = new List(); foreach (BlobItem x in source) { +#if NET472 downloadTasks.Add ( Task.Run(async () => { +#endif BlobClient blobClient = ReadableSource.GetBlobClient(x.Name); - BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync(); - BinaryData as1 = downloadResult.Content; - results.Add(as1.ToStream()); + BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync(); + BinaryData as1 = downloadResult.Content; + results.Add(as1.ToStream()); +#if NET472 }) ); +#endif } - bool hasFinished = false; #if NET472 + bool hasFinished = false; Task.Run(async () => -#endif { await Task.WhenAll(downloadTasks).ContinueWith(state => { hasFinished = !state.IsFaulted; }); } -#if NET472 ).Wait(); -#endif + if (!hasFinished) { Logger.LogError("Loading streams faulted."); throw new Exception("Did not read all blobs."); } +#endif // We discovered that sometimes getting blobs can return null streams... not helpful. Seems to be a race condition if (results.Count == source.Count && !results.Any(x => x == null)) break; + Thread.Sleep(150); results = null; } if (results == null)