user1178399
user1178399

Reputation: 1118

Upgraded EF Core to 3.1 and got compilation error for Task.WhenAll

In our project we used EF Core 2.2.6. But after upgrading to EF Core 3.1, the following piece of code stopped compiling and giving an error

var offerTask = DbContext.Offers.FindAsync(offerId);
var priceTask = DbContext.VendorPrices
                         .Where(x => x.OfferId == offerId && ...)
                         .ToListAsync(cancellationToken);
await Task.WhenAll(offerTask, priceTask);

Error CS1503
Argument 1: cannot convert from 'System.Threading.Tasks.ValueTask' to 'System.Threading.Tasks.Task'

I found out that in new version of EF Core, FindAsync method returns ValueTask (though in older version it returned Task), and this is causing the compilation error.

What is the workaround for this issue? Should I just use .AsTask()?

Upvotes: 0

Views: 310

Answers (1)

Okan Karadag
Okan Karadag

Reputation: 3055

Ef core 3.1 breaking changes :

Why

This change reduces the number of heap allocations incurred when invoking these methods, improving general performance. Applications simply awaiting the above APIs only need to be recompiled.

Mitigations

Applications simply awaiting the above APIs only need to be recompiled -no source changes are necessary. A more complex usage (e.g. passing the returned Task to Task.WhenAny()) typically require that the returned ValueTask<T> be converted to a Task<T> by calling AsTask() on it. Note that this negates the allocation reduction that this change brings.

Solution:

await Task.WhenAll(offerTask, priceTask).AsTask();

Upvotes: 0

Related Questions