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

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

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 :

Briefly,

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

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


Edit 2 :

  • Microsoft.CompilerServices.AsyncTargetingPack.Silverlight5.dll is added in my SL5 Client
  • using System . Threading . Tasks;

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.

I simply changed my project settings to use .Net Framework 4.5 or above and it worked.


You have to install Microsoft.Bcl.Async NuGet package to be able to use async/await constructs in pre-.NET 4.5 versions (such as Silverlight 4.0+)

Just for clarity - this package used to be called Microsoft.CompilerServices.AsyncTargetingPack and some old tutorials still refer to it.

Take a look here for info from Immo Landwerth.


Just experienced this in a method that executes a linq query.

public async Task<Foo> GetSomething()
{
    return await (from foo in Foos
                  select foo).FirstOrDefault();
}

Needed to use .FirstOrDefaultAsync() instead. N00b mistake.


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
}

Or, since the Client() method is 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
}

You could still use framework 4.0 but you have to include getawaiter for the classes:

MethodName(parameters).ConfigureAwait(false).GetAwaiter().GetResult();