This service takes in a report request object that can contain multiple reports that need to be rendered. This code works but I'm not sure if I've implemented anything wrong. Note the exportedImage.Result
makes this synchronous but when I tried adding async and await to the Parallel.ForEach
it doesn't seem to work so this was essentially a workaround, that's why I'm looking to figure out if I've implemented this correctly or not. _exporter.Export
is an asynchronous method.
Just FYI, when I tried to add async and await, I added it like so:
await Task.Run(() => Parallel.ForEach(request.Reports, async report =>
var exportedImage = await _exporter.Export(_reportServerUrl, reportPath, export, reportParameters);
var reportDictionary = new ConcurrentDictionary<int, string>();
await Task.Run(() => Parallel.ForEach(request.Reports, report =>
{
var (reportPath, bindingOrder, reportParameters) = report;
var exportedImage = _exporter.Export(_reportServerUrl, reportPath, export, reportParameters);
if (exportedImage.Result == null)
{
throw new InvalidOperationException($"Could not render report {reportIdentifier} with binding order {bindingOrder}.");
}
reportDictionary.GetOrAdd(bindingOrder, exportedImage.Result);
}));
async
/await
with the TPL methods (Parallel.
). They're two totally different approaches to scalability. \$\endgroup\$