I have a query class that implements Mediatr.IRequest like below:
public class ExportDataQuery : IRequest<IAsyncEnumerable<byte[]>> {}
The query handler has been implemented as follows:
public class ExportDataQueryHandler : IRequestHandler<ExportDataQuery, IAsyncEnumerable<byte[]>>
{
public async IAsyncEnumerable<byte[]> Handle(ExportDataQuery query, CancellationToken cancellationToken)
{
for (int page = 1; page <= pageCount; ++page)
{
// Get paginated data asynchronously.
var data = await _dbUtils.GetDataAsync(page, pageSize);
yield return data;
}
}
}
But I am getting the following build error on compiling the above code:
Error CS0738 'ExportDataQueryHandler' does not implement interface member 'IRequestHandler<ExportDataQuery, IAsyncEnumerable<byte[]>>.Handle(ExportDataQuery, CancellationToken)'. 'ExportDataQueryHandler.Handle(ExportDataQuery, CancellationToken)' cannot implement 'IRequestHandler<ExportDataQuery, IAsyncEnumerable<byte[]>>.Handle(ExportDataQuery, CancellationToken)' because it does not have the matching return type of 'Task<IAsyncEnumerable<byte[]>>'.
When I change return type of Handle to Task<IAsyncEnumerable<byte[]>>
, I get the following error:
Error CS1624 The body of 'ExportDataQueryHandler.Handle(ExportDataQuery, CancellationToken)' cannot be an iterator block because 'Task<IAsyncEnumerable<byte[]>>' is not an iterator interface type.
Is there any way to use yield return
in above request handler to return each page data one at a time?