.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Understanding Asynchronous Programming Model in Silverlight 4 with WCF RIA

Posted By:Manas Patnaik       Posted Date: January 18, 2011    Points: 75    Category: Silverlight    URL: http://www.dotnetspark.com  

Understanding Asynchronous Programming Model in Silverlight 4 with WCF RIA. If you are a newbie to Silverlight with RIA services and want to spent a little searching through forums/blog for some simple but crazy results.

Welcome Friends , . although it seems like a tabloid heading but the experience with Silverlight can be troublesome and frustrating if you ignore the async programming model/calls which  used predominantly in Silverlight and WCF RIA. The Async model changes logic  and flow of code creating confusion until you know about it.

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

Lets 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

lets be clear this is not exactly an issue but this is the way the RIA asynchronous programming works.Consider a case as bellow


Now not so difficult but surely you are going to pass through a frustrating stage if you ignore the rest of article.

The Normal way of doing

Normally once the popup closed we are reloading 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 problem for our above 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 using 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. ///
  12. /// CallBack Function for SubmitChanges
  13. ///
  14. ///
  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. ///
  2. /// CallBack Function for SubmitChanges
  3. ///
  4. ///
  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. }

This only one pick from the set of such operation 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 , by doing so not only you will save time but also makes her happy Smile,

hope my heading does make a sense , now.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend