Task<> does not contain a definition for 'GetAwaiter'
Asked Answered
K

14

90

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;
Kriegspiel answered 7/8, 2012 at 20:41 Comment(2)
Please state the C# version and include the usings from the top of the file.Cyrano
And if you're using the Async CTP, have you made sure to add a reference to the AsyncCtpLibrary.dll?Phosphorescence
T
25

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
}
Tollgate answered 7/8, 2012 at 21:30 Comment(0)
G
121

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.

Gaskin answered 9/9, 2015 at 9:20 Comment(2)
This was a case for me too. Changed from 4.0 to 4.5 and it worked.Faubourg
Or install Microsoft.Bcl.Async nuget package to remain in 4.0Strawboard
H
68

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.

Hannibal answered 5/7, 2017 at 17:41 Comment(2)
For me it was ToListAsync() . Worked great.Eustashe
Yes worked for me too - FirstOrDefaultAsync and adding a using Microsoft.EntityFrameworkCore; - Core 2.2 ProjectExemption
W
51

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.

Wolter answered 23/9, 2016 at 13:46 Comment(1)
thank you for this - the server I am using is 4.0 and I am unable to do an upgrade to 4.5Larena
T
25

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
}
Tollgate answered 7/8, 2012 at 21:30 Comment(0)
L
4

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

Lanta answered 26/11, 2019 at 6:17 Comment(0)
M
3

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

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

Mowbray answered 3/8, 2018 at 18:28 Comment(1)
ConfigureAwait also does not exist.Unprecedented
L
3

Make sure your .NET version 4.5 or greater

Latty answered 14/3, 2019 at 12:7 Comment(0)
S
2

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

Such answered 11/12, 2018 at 3:14 Comment(1)
Same, I am using UWP and it was just a missing using System; call.Rameriz
N
1

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

Neglectful answered 5/8, 2015 at 20:14 Comment(0)
U
1

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.

Upbeat answered 18/1, 2021 at 1:44 Comment(0)
A
0
public async Task<model> GetSomething(int id)
{
    return await context.model.FindAsync(id);
}
Angelia answered 22/8, 2017 at 11:56 Comment(1)
please add a description to your code explaining op why your code should help himElectrotechnics
W
0

If it's using an interface, check that the interface returns a Task<> too

Wilburwilburn answered 24/11, 2022 at 22:45 Comment(0)
C
-1

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;
Complect answered 22/12, 2020 at 22:14 Comment(0)
D
-1

Another way to reproduce this issue is:

await myvar = myFuncAsync(); 

Do this instead:

myvar = await myFuncAsync(); 
Ditzel answered 29/6, 2022 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.