does not contain a definition for 'GetAwaiter'

筅森魡賤 提交于 2019-12-23 10:07:43

问题


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


回答1:


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.




回答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.




回答3:


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;


来源:https://stackoverflow.com/questions/38570858/does-not-contain-a-definition-for-getawaiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!