Understanding Asynchronous Programming Model in Silverlight 4 with WCF RIA
Welcome friends! Although it seems like a tabloid heading, the experience with Silverlight can be troublesome and frustrating if you ignore the async programming model/calls which are 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 a must if you are a newbie to Silverlight with RIA services and want to spend a little time searching through forums/blog for some simple but crazy results.
Let's start with an example scenario, a typical issue where the listbox 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 that this is not exactly an issue but this is the way the RIA asynchronous programming works. Consider a case as below:

Now not so difficult but surely you are going to pass through a frustrating stage if you ignore the rest of the article.
The Normal way of Doing It
Normally once the popup closed, we are reloading the collection and binding to the list.
private void btnAddNew_Click(object sender, RoutedEventArgs e)
{
State stateObject = new State();
dataContext.States.Add(stateObject);
Views.AddNewState chldWind = new Views.AddNewState();
((Grid)chldWind.FindName("chldMainContainer")).DataContext = stateObject;
chldWind.Closed += new EventHandler(chldWind_Closed);
chldWind.Show();
}
void chldWind_Closed(object sender, EventArgs e)
{
ChildWindow chldWind = (ChildWindow)sender;
if ((chldWind.DialogResult == true))
{
dataContext.SubmitChanges();
}
else
dataContext.RejectChanges();
}
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 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 completes its task, the specified callback function is called where we can write our data load logic.
The Actual Way of Doing It
Instead of using SubmitChanges()
, we will use SubmitChanges Method (Generic Action, Object)
and we will invoke the data binding logic within the callback function.
void chldWind_Closed(object sender, EventArgs e)
{
ChildWindow chldWind = (ChildWindow)sender;
if ((chldWind.DialogResult == true))
{
dataContext.SubmitChanges(OnSubmitCompleted, null);
}
else
dataContext.RejectChanges();
}
private void OnSubmitCompleted(SubmitOperation so)
{
if (so.HasError)
{
MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
so.MarkErrorAsHandled();
}
LoadData();
}
As described above, the SubmitChange
line goes like this:
dataContext.SubmitChanges(OnSubmitCompleted, null);
While calling the following “OnSubmitCompleted
“ function once, the submit operation is over.
private void OnSubmitCompleted(SubmitOperation so)
{
if (so.HasError)
{
MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
so.MarkErrorAsHandled();
}
LoadData();
}
Conclusion
This is only one pick 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, by doing so not only will you save time, but it also makes her happy :).
Hope my heading does make sense now.