5

Im getting the below error with the below set of code, It is erroring on the 'alliancelookup' line, I'm not sure what i'm doing wrong but I couldn't see anything myself. The query im running to crest seems to be running fine but It seems the issue im having is with the Awaiter, I was wondering if there was a way around this?

DynamicCrest crest = new DynamicCrest();
var root = await crest.GetAsync(crest.Host);
var alliancelookup = await (await root.GetAsync(r => r.alliances)).First(i => i.shortName == e.GetArg("allianceticker").ToUpper());
allianceid = alliancelookup.id;

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Dynamic.ExpandoObject' does not contain a definition for 'GetAwaiter' at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at ***.Program.<>c.<b__2_10>d.MoveNext() in C:\Users---\Documents\Visual Studio 2015\Projects------\Program.cs:line 95

  • It would help to know what line 95 is. The error you get is pretty straight forward. What ever object you are trying to `await` does not properly implement the `GetAwaiter` method that is required for the while thing to work. – Nitram Jul 26 '16 at 09:04

3 Answers3

7

It's not possible to know exactly what's wrong without a minimal, complete, verifiable example, but it does look like you're awaiting something that isn't meant to be awaited.

Splitting up the alliancelookup line:

// Asynchronously retrieve the alliances.
var alliances = await root.GetAsync(r => r.alliances);

// Synchronously get the first matching one.
var allianceLookup = alliances.First(i => i.shortName == e.GetArg("allianceticker").ToUpper());

There may be a better approach, moving the filter into the async code, but that depends on DynamicCrest.

Community
  • 1
  • 1
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
2

You have one more await than what you need. You only have to await the async methods, but you also are awaiting the result, which apparently declared type is dynamic. That's why you are not getting a compile time error.

With dynamic it will try to bind the method that needs at runtime. That method when using async is .GetAwaiter(), an thus the RuntimeBinderException "does not contain a definition for 'GetAwaiter'"

That said, the third line should be:

var alliancelookup = (await root.GetAsync(r => r.alliances)).First(i => i.shortName == e.GetArg("allianceticker").ToUpper());

Although splitting up as suggested by Stephen Cleary is a better practice.

fbiagi
  • 766
  • 6
  • 11
  • This helped me. I was awaiting the async method's result (method.Result) which has declared type of dynamic. Removing the .Result worked. – lexma Sep 07 '20 at 17:52
0

You are awaiting a asyn call so you have to put async Task at the beginning of the method where you are calling this code

public async Task<returntyp> Name()
{
  DynamicCrest crest = new DynamicCrest();
  var root = await crest.GetAsync(crest.Host);
  var alliancelookup = await (await root.GetAsync(r => r.alliances)).First(i => i.shortName == e.GetArg("allianceticker").ToUpper());
  allianceid = alliancelookup.id;
Mostafiz
  • 7,243
  • 3
  • 28
  • 42