Bug report: RuntimeBinderException: 'object' does not contain a definition for 'Task'
LinqPad 8.9.5 (x64) running on Windows.
The following script fails from time to time (I think you may have to run it once, then make some minor changes to the script, like add and remove some braces, then hit F5).
RuntimeBinderException
Message: 'object' does not contain a definition for 'Task'
StackTrace:
at CallSite.Target(Closure, CallSite, Object)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at LINQPad.Util.CacheAsync[T](Func1 dataFetcher, String key, Boolean forceRefresh) at LINQPad.Util.CacheAsync[T](Func
1 dataFetcher)
at UserQuery.Main(), line 7
async Task Main() { var counts = new[] { 1, 4, 8, 2, 4 }; var tasks = counts.Select(async c => await (new DataLoader(c)).GetSomeData()); var sortedData = await Util.CacheAsync(async () => (await Task.WhenAll(tasks)) .SelectMany(x => x) .OrderBy(x => x.Value) .ToList() ); sortedData.Dump(); } class DataLoader { private readonly int _count; public DataLoader(int count) { _count = count; } public async Task<List<Foo>> GetSomeData() { var data = new List<Foo>(); for (var i = 0; i < _count; ++i) { data.Add(new Foo(Random.Shared.Next())); await Task.Delay(500); } return data; } } class Foo { public Foo(int value) { Value = value; } public int Value { get; } }
Is it better to post things like this here or use the "Help -> Report Bug" option?
Comments
-
Rewriting with
Util.Cache
fixed it (and then of course I had to move the data loading into the delegate/lambda expression as well, but that's another matter):var counts = new[] { 1, 4, 8, 2, 4 }; var sortedData = Util.Cache(() => { var tasks = counts.Select(async c => await (new DataLoader(c)).GetSomeData()); return Task.WhenAll(tasks) .Result .SelectMany(x => x) .OrderBy(x => x.Value) .ToList(); });
I'm not sure if this is a bug or just something I'm not understanding?
The above code with CacheAsync also works, just not reliable. Which makes me think it's a bug, probably something about the code tree being generated...? -
Unfortunately, it doesn't look like the updates needed for the cache converter have been made yet.
See: https://forum.linqpad.net/discussion/comment/8929/#Comment_8929
-
Ah, good catch! Thanks.
But I'm not even using any dictionaries or complex types here...? -
I think a lot of changes will need to be made with regards to caching since it was first reported. I bet there were a lot of changes everywhere that it's probably a more complex problem by now.
-
Well, at least it works for me with Util.Cache now