I am trying to use the .NET Reactive Framework to simplify some of the asynchronous calls to the WCF service used by the Silverlight 3 application that I am writing.
The problem is that it's hard for me to find a way to structure my code in a way that will work. Part of the problem, no doubt, understands what mechanisms are available in Reactive and how to use them to solve my problem.
I am trying to combine 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( ... );
goto case GameVisualState.HandNotStarted;
case GameVisualState.HandNotStarted:
case GameVisualState.HandCompleted:
case GameVisualState.HandSurrendered:
UpdateUIMechanics();
ChangeVisualState( GameVisualState.HandPlaceBet );
break;
case GameVisualState.HandPlaceBet:
UpdateUIMechanics();
m_GameState = m_Server.NextHand( m_GameState, CurrentBetAmount );
if( CertainConditionInGameState( m_GameState ) )
m_GameState = m_Server.CompleteHand( m_GameState );
break;
}
m_Server.XXXX() Silveright ( , ), WCF. Silverlight WCF - .
Observable.FromEvent<>() XXXCompleted, - WCF, , . :
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( ... );
TransitionVisual( GameVisualState.HandNotStarted );
} );
m_Server.StartGameAsync();