任务<>不包含'GetAwaiter“的定义 [英] Task<> does not contain a definition for 'GetAwaiter'

查看:13555
本文介绍了任务<>不包含'GetAwaiter“的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户端

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 );
}

合同

    [OperationContract ( AsyncPattern = true )]
    IAsyncResult BeginConnection ( SerializableDynamicObject Message , AsyncCallback Callback , object State );

    SerializableDynamicObject EndConnection ( IAsyncResult Result );

服务

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;
}

从Silverlight客户端暴露服务

Exposing Service from Silverlight Client

private async void myButton ( object sender , RoutedEventArgs e )
{
    dynamic Request = new SerializableDynamicObject ( );
    Request . Operation = "test";

    var task = Client ( Request );
    var result = await task;  // <------------------------------ Exception
}

例外

Task<SerializableDynamicObject > does not contain a definition for 'GetAwaiter'

什么的错?

编辑1:

简单地说,

的Visual Studio 2012 RC的的Silverlight 5应用消耗的游戏 WCF服务4 在主持的 ASP.net 4应用通过共享的ChannelFactory技术的便携式图书馆.NET4 / SL5 包含的的iGame 的带接口的异步CTP

Visual studio 2012 RC Silverlight 5 Application consumes Game WCF 4 Service hosted in ASP.net 4 Application with ChannelFactory technique via Shared Portable Library .NET4/SL5 contains the iGame interface with Async CTP

图:结果
ASP.NET的&LT; = 类库(游戏)的&LT; = 便携式库(的iGame) => 的Silverlight

Graph :
ASP.NET <= Class Library ( Game ) <= Portable Library ( iGame ) => Silverlight

编辑2:


  • Microsoft.CompilerServices.AsyncTargetingPack.Silverlight5.dll在我SL5客户端添加

  • 使用系统。线程。任务;

推荐答案

GetAwaiter(),所使用的等待,被实现为在异步CTP的扩展方法。我不知道你到底是使用的是什么(你在你的问题提两个异步CTP和VS 2012 RC),但它是可能的异步针对包使用同样的技术。

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.

问题则是,扩展方法不动态工作。你可以做的是要明确指定你与工作,这意味着扩展方法将工作的工作,然后再切换回动态

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:

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
}

或者,因为客户端()方法实际上不是不是动态的,可以用 SerializableDynamicObject 调用它,没有动态等限制使用动态尽可能的:

Or, since the Client() method is not actually not dynamic, you could call it with SerializableDynamicObject, not dynamic, and so limit using dynamic as much as possible:

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
}

这篇关于任务&LT;&GT;不包含'GetAwaiter“的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆