New to Telerik ReportingStart a free 30-day trial

Using Async Methods with ObjectDataSource

Environment

ProductProgress® Telerik® Reporting

Description

When configuring an ObjectDataSource with an async method that returns a Task<T>, the ObjectDataSource wizard displays Task members (such as AsyncState, CreationOptions, IsCanceled, etc.) instead of the actual data properties.

Cause

The ObjectDataSource component does not currently support async methods. The reporting engine expects synchronous data retrieval and does not automatically await Task objects returned by async methods.

Solution

If possible, use synchronous methods for data retrieval with ObjectDataSource.

If you need to work with existing async methods, you can create a synchronous wrapper method:

C#
public class MyData
{
    public int Id { get; set; }
    public string Name { get; set; }

    public static List<MyData> GetData()
    {
        return GetDataAsync().GetAwaiter().GetResult();
    }

    static Task<List<MyData>> GetDataAsync()
    {
        var resultList = new List<MyData>
        {
            new MyData { Id = 1, Name = "One" },
            new MyData { Id = 2, Name = "Two" },
            new MyData { Id = 3, Name = "Three" },
        };

        return Task.FromResult(resultList);
    }
}

In the ObjectDataSource configuration or ObjectDataSource Wizard, use the synchronous GetData() method instead of GetDataAsync().

See Also