使用Observable.FromEvent调用在Silverlight WCF服务时, [英] Using Observable.FromEvent when calling a WCF service in Silverlight

查看:126
本文介绍了使用Observable.FromEvent调用在Silverlight WCF服务时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用.NET框架无功,以简化由一个Silverlight 3应用程序,我正在写一个使用WCF服务的一些异步调用。

麻烦的是,我有一个很难找到一个方法来组织我的code的方式,将工作。问题的部分原因,无疑是了解什么机制在无可用以及如何使用它们来解决我的问题。

我想串起来的一系列WCF服务器调用的 - 如果他们是同步的,他们会是这个样子:

 开关(CurrentVisu​​alState)
{
    案例GameVisualState.Welcome:
        m_gameState = m_Server.StartGame();
        如果(m_GameState.Bankroll< Game.MinimumBet)
            NotifyPlayer(...); //一些UI code这里...
        转到情况下GameVisualState.HandNotStarted;    案例GameVisualState.HandNotStarted:
    案例GameVisualState.HandCompleted:
    案例GameVisualState.HandSurrendered:
        UpdateUIMechanics();
        ChangeVisualState(GameVisualState.HandPlaceBet);
        打破;    案例GameVisualState.HandPlaceBet:
        UpdateUIMechanics();
        //从游戏服务器请求更新的游戏状态...
        m_GameState = m_Server.NextHand(m_GameState,CurrentBetAmount);
        如果(CertainConditionInGameState(m_GameState))
            m_GameState = m_Server.CompleteHand(m_GameState);
        打破;
}

通话m_Server.XXXX()用于直接在Silveright应用程序(因此他们可能是同步)中实现的 - 但现在的WCF服务中实现。由于Silverlight迫使你调用WCF服务的异步 - 重写code的此块一直棘手

我希望用 Observable.FromEvent<>()订阅各种 XXXCompleted 事件在WCF代理code产生,但它是我不清楚如何得到这个工作。我原来的企图看起来是这样的:

  VAR startObs = Observable.FromEvent< StartGameCompletedEventArgs>(
                  H => m_Server.StartGameCompleted + = H,
                  H => m_Server.StartGameCompleted - = H);startObs.Subscribe(E => {m_gameState = e.EventArgs.Result.StartGameResult;
                           如果(m_GameState.Bankroll< Game.MinimumBet)
                               NotifyPlayer(...); //一些UI code这里...
                           TransitionVisual(GameVisualState.HandNotStarted);
                         }); //上述code从未达到过...m_Server.StartGameAsync(); //永远不会返回,但WCF服务被称为


解决方案

我能弄清楚如何得到这个工作。我张贴在分享我所学到的利息这个答案。

原来,决定哪些线程上执行订阅观察者处理Silverlight的WCF调用的时候是非常重要的。就我而言,我需要确保订阅code中的UI线程上运行 - 这是由以下变化完成的:

  VAR startObs = Observable.FromEvent< StartGameCompletedEventArgs>(
                  H => m_Server.StartGameCompleted + = H,
                  H => m_Server.StartGameCompleted - = H)
        。取(1)//必要,以确保观察到的取消订阅
        .ObserveOnDispatcher(); //观察者运行在哪个线程控制startObs.Subscribe(E => {m_gameState = e.EventArgs.Result.StartGameResult;
                           如果(m_GameState.Bankroll< Game.MinimumBet)
                               NotifyPlayer(...); //一些UI code这里...
                           TransitionVisual(GameVisualState.HandNotStarted);
                         }); //这个code现在可以访问用户界面执行m_Server.StartGameAsync(); //启动调用WCF服务

I am trying to use the .NET Reactive Framework to simplify some asynchronous calls to a WCF service used by a Silverlight 3 app that I'm writing.

The trouble is that I'm having a hard time finding a way to structure my code in a way that will work. Part of the problem, no doubt, is understanding what mechanisms are available in Reactive and how to use them to solve my problem.

I'm trying to string together a series of WCF server calls - if they were synchronous, they would look something like this:

switch( CurrentVisualState )
{
    case GameVisualState.Welcome:
        m_gameState = m_Server.StartGame();
        if( m_GameState.Bankroll < Game.MinimumBet )
            NotifyPlayer( ... );  // some UI code here ...
        goto case GameVisualState.HandNotStarted;

    case GameVisualState.HandNotStarted:
    case GameVisualState.HandCompleted:
    case GameVisualState.HandSurrendered:
        UpdateUIMechanics();
        ChangeVisualState( GameVisualState.HandPlaceBet );
        break;

    case GameVisualState.HandPlaceBet:
        UpdateUIMechanics();
        // request updated game state from game server...
        m_GameState = m_Server.NextHand( m_GameState, CurrentBetAmount );
        if( CertainConditionInGameState( m_GameState ) )
            m_GameState = m_Server.CompleteHand( m_GameState );
        break;
}

The calls to m_Server.XXXX() used to be implemented directly within the Silveright app (thus they could be synchronous) - but now are implemented within a WCF service. Since Silverlight forces you to call WCF services asynchronously - rewriting this block of code has been tricky.

I was hoping to use Observable.FromEvent<>() to subscribe to the various XXXCompleted events that the WCF proxy code generates, but it's unclear to me how to get this to work. My original attempt looked something like:

var startObs = Observable.FromEvent<StartGameCompletedEventArgs>(
                  h => m_Server.StartGameCompleted += h,
                  h => m_Server.StartGameCompleted -= h );

startObs.Subscribe( e => { m_gameState = e.EventArgs.Result.StartGameResult;
                           if( m_GameState.Bankroll < Game.MinimumBet )
                               NotifyPlayer( ... );  // some UI code here ...
                           TransitionVisual( GameVisualState.HandNotStarted );
                         } );  // above code never reached...

m_Server.StartGameAsync();  // never returns, but the WCF service is called

解决方案

I was able to figure out how to get this to work. I am posting this answer in the interest of sharing what I've learned.

It turns out that deciding which thread to execute a subscribed observer on is very important when dealing with Silverlight WCF calls. In my case, I needed to ensure that the subscribed code runs on the UI thread - which was accomplished by the following change:

var startObs = Observable.FromEvent<StartGameCompletedEventArgs>(
                  h => m_Server.StartGameCompleted += h,
                  h => m_Server.StartGameCompleted -= h )
        .Take(1) // necessary to ensure the observable unsubscribes
        .ObserveOnDispatcher(); // controls which thread the observer runs on

startObs.Subscribe( e => { m_gameState = e.EventArgs.Result.StartGameResult;
                           if( m_GameState.Bankroll < Game.MinimumBet )
                               NotifyPlayer( ... );  // some UI code here ...
                           TransitionVisual( GameVisualState.HandNotStarted );
                         } );  // this code now executes with access to the UI

m_Server.StartGameAsync();  // initiates the call to the WCF service

这篇关于使用Observable.FromEvent调用在Silverlight WCF服务时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆