[c#] 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;

This question is related to c# wcf silverlight-5.0 async-ctp

The answer is


I had this problem because I was calling a method

await myClass.myStaticMethod(myString);

but I was setting myString with

var myString = String.Format({some dynamic-type values})

which resulted in a dynamic type, 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!

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 myStaticMethod routed properly


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.


In my case just add using System; solve the issue.


I had the same issue, I had to remove the async keyword from the method and also remove the await keyword from the calling method to use getawaiter() without any error.


The Problem Occur Because the application I was using and the dll i added to my application both have different Versions.

Add this 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


In case it helps anyone, the error message:

'does not contain a definition for 'GetAwaiter'' etc.

also occurs if the await is incorrectly placed before the variable like this:

enter image description here

instead of:

RtfAsTextLines = await OpenNoUserInterface(FilePath);

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.


public async Task<model> GetSomething(int id)
{
    return await context.model.FindAsync(id);
}

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

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


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.


If you are writing a Visual Studio Extension (VSIX) then ensure that you have a using statement for Microsoft.VisualStudio.Threading, as such:

using Microsoft.VisualStudio.Threading;

Make sure your .NET version 4.5 or greater


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to wcf

Create a asmx web service in C# using visual studio 2013 WCF Exception: Could not find a base address that matches scheme http for the endpoint WCF Service, the type provided as the service attribute values…could not be found WCF error - There was no endpoint listening at How can I pass a username/password in the header to a SOAP WCF Service The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'NTLM' Content Type application/soap+xml; charset=utf-8 was not supported by service The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) maxReceivedMessageSize and maxBufferSize in app.config how to generate a unique token which expires after 24 hours?

Examples related to silverlight-5.0

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

Examples related to async-ctp

Task<> does not contain a definition for 'GetAwaiter' How to call an async method from a getter or setter?