客户
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 客户端公开服务
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 应用程序使用通过共享便携式库使用 ChannelFactory 技术托管在ASP.net 4 应用程序中的Game WCF 4 服务。NET4/SL5包含带有异步 CTP的iGame接口
图:
ASP.NET <=类库 (Game) <=便携式库 (iGame) => Silverlight
编辑 2:
- Microsoft.CompilerServices.AsyncTargetingPack.Silverlight5.dll 添加到我的 SL5 客户端
- 使用系统。穿线。任务;