C# 任务<> 不包含“GetAwaiter”的定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11853812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Task<> does not contain a definition for 'GetAwaiter'
提问by Ahmed Ghoneim
Client
客户
iGame Channel = new ChannelFactory<iGame> ( new BasicHttpBinding ( BasicHttpSecurityMode . None ) , new EndpointAddress ( new Uri ( "http://localhost:58597/Game.svc" ) ) ) . CreateChannel ( );
public Task<SerializableDynamicObject> Client ( SerializableDynamicObject Packet )
{
return Task<SerializableDynamicObject> . Factory . FromAsync ( Channel . BeginConnection , Channel . EndConnection , Packet , null );
}
Contract
合同
[OperationContract ( AsyncPattern = true )]
IAsyncResult BeginConnection ( SerializableDynamicObject Message , AsyncCallback Callback , object State );
SerializableDynamicObject EndConnection ( IAsyncResult Result );
Service
服务
public IAsyncResult BeginConnection ( SerializableDynamicObject Message , AsyncCallback Callback , object State )
{
dynamic Request = Message;
dynamic Response = new SerializableDynamicObject ( );
if ( Request . Operation = "test" )
{
Response . Status = true;
}
Response . Status = false;
return new CompletedAsyncResult<SerializableDynamicObject> ( Response );
}
public SerializableDynamicObject EndConnection ( IAsyncResult Result )
{
return ( Result as CompletedAsyncResult<SerializableDynamicObject> ) . Data;
}
Exposing Service from Silverlight Client
从 Silverlight 客户端公开服务
private async void myButton ( object sender , RoutedEventArgs e )
{
dynamic Request = new SerializableDynamicObject ( );
Request . Operation = "test";
var task = Client ( Request );
var result = await task; // <------------------------------ Exception
}
Exception
例外
Task<SerializableDynamicObject > does not contain a definition for 'GetAwaiter'
What's wrong ?
怎么了?
Edit 1 :
编辑1:
Briefly,
简要地,
Visual studio 2012 RC Silverlight 5 Applicationconsumes GameWCF 4 Servicehosted in ASP.net 4 Applicationwith ChannelFactory technique via Shared Portable Library .NET4/SL5contains the iGameinterface with Async CTP
的Visual Studio 2012 RC的Silverlight 5应用程序消耗游戏WCF 4服务托管在ASP.net 4应用程序通过共享与技术的ChannelFactory移植库.NET4 / SL5包含的iGame与界面异步CTP
Graph :
ASP.NET <=Class Library ( Game ) <=Portable Library ( iGame ) =>Silverlight
图:
ASP.NET <=类库(游戏)<=便携式库(iGame)=>Silverlight
Edit 2 :
编辑2:
- Microsoft.CompilerServices.AsyncTargetingPack.Silverlight5.dll is added in my SL5 Client
- using System . Threading . Tasks;
- Microsoft.CompilerServices.AsyncTargetingPack.Silverlight5.dll 添加到我的 SL5 客户端
- 使用系统。穿线。任务;
采纳答案by svick
GetAwaiter(), that is used by await, is implemented as an extension method in the Async CTP. I'm not sure what exactly are you using (you mention both the Async CTP and VS 2012 RC in your question), but it's possible the Async targeting pack uses the same technique.
GetAwaiter(),由 使用await,作为异步 CTP 中的扩展方法实现。我不确定您到底在使用什么(您在问题中提到了 Async CTP 和 VS 2012 RC),但 Async 目标包可能使用相同的技术。
The problem then is that extension methods don't work with dynamic. What you can do is to explicitly specify that you're working with a Task, which means the extension method will work, and then switch back to dynamic:
那么问题是扩展方法不适用于dynamic. 您可以做的是明确指定您正在使用 a Task,这意味着扩展方法将起作用,然后切换回dynamic:
private async void MyButtonClick(object sender, RoutedEventArgs e)
{
dynamic request = new SerializableDynamicObject();
request.Operation = "test";
Task<SerializableDynamicObject> task = Client(request);
dynamic result = await task;
// use result here
}
Or, since the Client()method is actually not dynamic, you could call it with SerializableDynamicObject, not dynamic, and so limit using dynamicas much as possible:
或者,由于该Client()方法实际上不是动态的,您可以使用SerializableDynamicObject、not调用它dynamic,因此dynamic尽可能多地限制使用:
private async void MyButtonClick(object sender, RoutedEventArgs e)
{
var request = new SerializableDynamicObject();
dynamic dynamicRequest = request;
dynamicRequest.Operation = "test";
var task = Client(request);
dynamic result = await task;
// use result here
}
回答by Sean
I had this problem because I was calling a method
我有这个问题,因为我正在调用一个方法
await myClass.myStaticMethod(myString);
but I was setting myString with
但我正在设置 myString
var myString = String.Format({some dynamic-type values})
which resulted in a dynamictype, not a string, thus when I tried to await on myClass.myStaticMethod(myString), the compiler thought I meant to call myClass.myStaticMethod(dynamic myString). This compiled fine because, again, in a dynamic context, it's all good until it blows up at run-time, which is what happened because there is no implementation of the dynamic version of myStaticMethod, and this error message didn't help whatsoever, and the fact that Intellisense would take me to the correct definition didn't help either.
Tricky!
这导致了一个dynamic类型,而不是一个string,因此当我试图等待时myClass.myStaticMethod(myString),编译器认为我打算调用myClass.myStaticMethod(dynamic myString). 这编译得很好,因为同样,在动态上下文中,一切都很好,直到它在运行时爆炸,这是因为没有实现 的动态版本myStaticMethod,并且此错误消息没有任何帮助,并且Intellisense 将我带到正确定义的事实也无济于事。
棘手!
However, by forcing the result type to string, like:
但是,通过强制结果类型为字符串,例如:
var myString = String.Format({some dynamic-type values})
to
到
string myString = String.Format({some dynamic-type values})
my call to myStaticMethodrouted properly
我的呼叫myStaticMethod路由正确
回答by Zapnologica
I had this issue in one of my projects, where I found that I had set my project's .Net Framework version to 4.0 and async tasks are only supported in .Net Framework 4.5 onwards.
我在我的一个项目中遇到了这个问题,我发现我已将项目的 .Net Framework 版本设置为 4.0,并且异步任务仅在 .Net Framework 4.5 及以后的版本中受支持。
I simply changed my project settings to use .Net Framework 4.5 or above and it worked.
我只是将我的项目设置更改为使用 .Net Framework 4.5 或更高版本并且它工作正常。
回答by Pawel Hofman
You have to install Microsoft.Bcl.AsyncNuGet package to be able to use async/awaitconstructs in pre-.NET 4.5 versions (such as Silverlight 4.0+)
您必须安装Microsoft.Bcl.AsyncNuGet 包才能async/await在 .NET 4.5 之前的版本(例如 Silverlight 4.0+)中使用构造
Just for clarity - this package used to be called Microsoft.CompilerServices.AsyncTargetingPackand some old tutorialsstill refer to it.
只是为了清楚起见 - 这个包曾经被称为Microsoft.CompilerServices.AsyncTargetingPack,一些旧教程仍然引用它。
Take a look herefor info from Immo Landwerth.
看看这里从IMMO Landwerth信息。
回答by JOpuckman
Just experienced this in a method that executes a linq query.
刚刚在执行 linq 查询的方法中经历了这一点。
public async Task<Foo> GetSomething()
{
return await (from foo in Foos
select foo).FirstOrDefault();
}
Needed to use .FirstOrDefaultAsync()instead. N00b mistake.
需要.FirstOrDefaultAsync()改用。N00b 错误。
回答by Ponnuru Sivavaraprasad
public async Task<model> GetSomething(int id)
{
return await context.model.FindAsync(id);
}
回答by Abhi
You could still use framework 4.0 but you have to include getawaiterfor the classes:
您仍然可以使用框架 4.0,但您必须包含getawaiter这些类:
MethodName(parameters).ConfigureAwait(false).GetAwaiter().GetResult();
MethodName(parameters).ConfigureAwait(false).GetAwaiter().GetResult();
回答by user632095
In my case just add using System;solve the issue.
在我的情况下,只需添加using System;解决问题。
回答by Oranit Dar
Make sure your .NET version 4.5 or greater
确保您的 .NET 版本为 4.5 或更高版本
回答by Purnima Bhatia
The Problem Occur Because the application I was using and the dll i added to my application both have different Versions.
出现问题是因为我使用的应用程序和我添加到应用程序中的 dll 都有不同的版本。
Add this Package- Install-Package Microsoft.Bcl.Async -Version 1.0.168
添加这个 Package-Install-Package Microsoft.Bcl.Async -Version 1.0.168
ADDING THIS PACKAGE async Code becomes Compatible in version 4.0 as well, because Async only work on applications whose Versions are more than or equal to 4.5
添加此包异步代码在 4.0 版中也变得兼容,因为 Async 仅适用于版本大于或等于 4.5 的应用程序

