I try to execute this code async and parallel by using the ParallelForEachAsync
method from this project: Dasync/AsyncEnumerable. Unfortunately the method never returns.
The SampleProduct
is a simple DTO which has a boolean property and two string properties.
The GetOsmData
method tries to get data via http-request, but often throws an exception.
My first attempt was without .ConfigureAwait(false)
, but has the same result...
If I try this method with a list of products (products.Count = 8
) result.Count
always stops by 7.
private async Task<ConcurrentBag<SampleProduct>> CheckOsmDataAsync(
List<SampleProduct> products)
{
var result = new ConcurrentBag<SampleProduct>();
await products.ParallelForEachAsync(
async product =>
{
OsmData osmData;
try
{
osmData = await GetOsmData(_osmUrl.Replace("articlenumber",
product.MaterialNumber.ToString())).ConfigureAwait(false);
}
catch (Exception e)
{
osmData = null;
}
if (osmData != null && osmData.PrintingData.Count > 0)
{
product.OsmPrintImageAvailable = true;
}
else
{
product.OsmPrintImageAvailable = false;
}
result.Add(product);
},
// 0 => Chooses a default value based on the processor count
maxDegreeOfParallelism: 0
);
return result;
}
With the help of my colleague I could solve the problem... The problem was not inside the method itself, but only how it was called. I called it form the Main/UI-Thread synchronously. That seems to have caused a deadlock. Making the calling method async and awaiting the CheckOsmDataAsync() solved the problem.
Nevertheless thanks for your responds!