Task<> does not contain a definition for 'G

2019-01-11 16:02发布

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;

8条回答
地球回转人心会变
2楼-- · 2019-01-11 16:28

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

查看更多
Evening l夕情丶
3楼-- · 2019-01-11 16:30

I had this issue in one of my projects, where I found that I had set my projects .net version to 4.0 and async tasks are only supported in .net 4.5 on wards.

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

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-11 16:30
public async Task<model> GetSomething(int id)
{
    return await context.model.FindAsync(id);
}
查看更多
Explosion°爆炸
5楼-- · 2019-01-11 16:39

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.

查看更多
Deceive 欺骗
6楼-- · 2019-01-11 16:48

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-11 16:51

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

查看更多
登录 后发表回答