New to Telerik Reporting? Download free 30-day trial

How to Use Async Methods in Report Event Handlers

Environment

Product Progress® Telerik® Reporting

Description

The report rendering is synchronous process as the items need to be rendered in order. Async/Await have been introduced with C# 5.0 and .NET Framework 4.5. Reporting engine code targets .NET 4 and the report events are not marked as async. That is why it is not possible to use await inside the report events.
If you already have async methods, for example, for fetching data and you want to use them as data providers for you reports, you may use the approach described in this article.

Important Note

When using async over a synchronous call, there is a chance for a deadlock, i.e. blocking of the UI thread. For that reason, we don't recommend this approach. It is preferable to re-create the async methods as synchronous.

Workaround

Use the following syntax to call your async method, lets name it MyMethodAsync, in the report event handler or other synchronously executed code:

MyMethodAsync().ConfigureAwait(false).GetAwaiter().GetResult();

In the above code, ConfigureAwait(false) configures the task so that continuation after the await does not have to be run in the caller/UI context, therefore avoiding deadlocks. When everyting runs fine and there are no exceptions, the myTask.GetAwaiter().GetResult(); is equivallent to myTask.Result;. However, with the latter apporach, the potential exception will be wrapped in an AggregateException, whereas the former approach throws the direct exception. This makes the syntax from the code snippet the most preferable with respect to deadlock prevention and error handling.

See Also

C#: Why you should use ConfigureAwait(false) in your library code
Task.Result Follow Task.GetAwaiter.GetResult Is () the same? How to choose?

In this article