1

简要地,

Visual Studio 2012 RC Silverlight 5 应用程序使用通过共享便携式库使用 ChannelFactory 技术托管在ASP.net 4 应用程序中的Game WCF 4 服务。NET4/SL5包含带有异步 CTP的iGame接口

图:
ASP.NET <=类库 (Game) <=便携式库 (iGame) => Silverlight

便携式图书馆

[ServiceContract]
public interface iGame
{
    [OperationContract]
    Task<bool> Request ( string Key );
}

类库

[ServiceBehavior ( InstanceContextMode = InstanceContextMode . Single , ConcurrencyMode = ConcurrencyMode . Multiple , UseSynchronizationContext = true )]
public class Game : iGame
{
    public async Task<bool> Request ( string Key )
    {
        return await Task . Factory . StartNew ( ( ) => true );
    }
}

银光

    private async void myButton_Click ( object sender , RoutedEventArgs e )
    {
        if ( await Messenger . Instance . Client . Request ( XXX . Text ) ) // Exception
            NavigationService . Navigate ( new Uri ( "/Views/YYY.xaml" , UriKind . Relative ) );
    }
  • Messenger 是一个单例类,通过 ChannelFactory 启动和存储我的客户端代理。

System.InvalidOperationException: The contract 'iGame' contains synchronous operations, which are not supported in Silverlight. Split the operations into "Begin" and "End" parts and set the AsyncPattern property on the OperationContractAttribute to 'true'. Note that you do not have to make the same change on the server.
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state)

怎么了?o_o

4

1 回答 1

2

在您的客户端,您的iGame代理是同步的,并且您在它周围使用假异步包装器假装它是异步的。

您需要一个异步代理。您可以通过让 VS2012RC 重新生成代理来做到这一点,或者您可以使用TaskWsdlImportExtension. 我不确定这两种解决方案是否可以在便携式库中使用。

于 2012-08-05T18:09:15.893 回答