Asynchronous Programming Model in Silverlight 4 with WCF RIA

Save your time by Understanding Asynchronous Programming Model in Silverlight 4 with WCF RIA


The use of Silverlight can be troubling and frustrating if you ignore the async programming model/calls that are often used in Silverlight and WCF RIA. The Async model does affect the logic and flow of code, thereby causing confusion until you understand it.

So this article is a must if you are a newbie to Silverlight with RIA services and want to spend a little time searching through forums/blogs for some simple but crazy results.

Let's start with a example scenario; a typical issue where the list box didn't update although we used to re-fetch the entity collection after adding some entity, using domainservicecontext.

The Issue and Scenario

Let's be clear; this is not exactly an issue but this is the way the RIA asynchronous programming works. Consider a case as follows:

SNAGHTML9622ed

It looks simple but surely you will go through a period of frustration if you ignore the remainder of this article.

THE NORMAL WAY OF DOING
 

Normally once the popup is closed we are reloading the collection and binding to the list.

  1. private void btnAddNew_Click(object sender, RoutedEventArgs e)
  2. {
  3. //Create a New State Object
  4. State stateObject = new State();
  5. //Add to the StateCollection of Domain DataContext
  6. dataContext.States.Add(stateObject);
  7. //Create a PopUp Child Window Object
  8. Views.AddNewState chldWind = new Views.AddNewState();
  9. //Getting the GridLayout Control of the popUp To Be Shown and assign the Newly Created Object
  10. ((Grid)chldWind.FindName("chldMainContainer")).DataContext = stateObject;
  11. //Attach The Todo on Close Event handaler
  12. chldWind.Closed += new EventHandler(chldWind_Closed);
  13. chldWind.Show();
  14. }
  15. void chldWind_Closed(object sender, EventArgs e)
  16. {
  17. ChildWindow chldWind = (ChildWindow)sender;
  18. if ((chldWind.DialogResult == true))
  19. {
  20. dataContext.SubmitChanges();
  21. }
  22. else
  23. dataContext.RejectChanges();
  24. //Refresh The List Box Data
  25. //*** Your Code to Load Re Fetch the Data***
  26. }


But wait; this will not work. This is where you need to be aware of async call.

UNDERSTANDING ASYNCHRONOUS OPERATION
"In asynchronous mode an application will continue with other command or functions while the previously command is still in execution mode"

Now this creates a problem for our previosu sample. We called dataContext.SubmitChanges(); then in the next line we are binding the list box item Source. We need to wait for the async call "submitchanges" to be finished. But how do we will handle it ????? Simple; use CallBack Functions.

Once the command complete its task the specified call back function is called where we can write our data load logic.

The Actual way of doing
 

Instead of using SubmitChanges() we will use SubmitChanges Method (Generic Action, Object) and we will invoke the data binding logic with in the call back function.
 

  1. void chldWind_Closed(object sender, EventArgs e)
  2. {
  3. ChildWindow chldWind = (ChildWindow)sender;
  4. if ((chldWind.DialogResult == true))
  5. {
  6. dataContext.SubmitChanges(OnSubmitCompleted, null);
  7. }
  8. else
  9. dataContext.RejectChanges();
  10. }
  11. /// <summary>
  12. /// CallBack Function for SubmitChanges
  13. /// </summary>
  14. /// <param name="so"></param>
  15. private void OnSubmitCompleted(SubmitOperation so)
  16. {
  17. if (so.HasError)
  18. {
  19. MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
  20. so.MarkErrorAsHandled();
  21. }
  22. //Logic to Bind Data to List Box
  23. LoadData();
  24. }


As described above the SubmitChange line goes like this

  1. dataContext.SubmitChanges(OnSubmitCompleted, null);


While calling the following "OnSubmitCompleted " function once the submit operation is over.

  1. /// <summary>
  2. /// CallBack Function for SubmitChanges
  3. /// </summary>
  4. /// <param name="so"></param>
  5. private void OnSubmitCompleted(SubmitOperation so)
  6. {
  7. if (so.HasError)
  8. {
  9. MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
  10. so.MarkErrorAsHandled();
  11. }
  12. //Logic to Bind Data to List Box
  13. LoadData();
  14. }

Conclusion
 

This is only one choice from the set of such operations; another example I can mention here is the way the collection is getting loaded using LoadOperation by executing the Entity Query.

So keep Async Operations in mind while coding with SL and WCF RIA service.