ClientContext cleared during simultaneous ViewModel refresh

ClientContext cleared during simultaneous ViewModel refresh

Old forum URL:

richardp posted on Wednesday, September 21, 2011

Hi, I am using CSLA 4.1.0 for a Silverlight app.

I recently experienced a problem where my ClientContext was being randomly cleared.

After a lot of digging I have traced the source of the problem (I hope).

It is because I am refreshing several ViewModel objects simultaneously.

I am using the MVVM pattern.

I have a main page user control with several child user controls on it.

Several of the child user controls have their own dedicated view models.

When the main page loads it sets off initialization of the child view models simultaneously.

They are all attempting to refresh using the ViewModel method "BeginRefresh".

(It is easy to get into this situation in Silverlight with everything being async.)


It seems there is some sort of race condition and the ClientContext sometimes gets cleared by the DataPortal at the wrong time.


I have re-worked the code so that all calls to the DataPortal are sequential and the code is working with no probs.


I was wondering if the CSLA is designed so that only one call to the DataPortal can be active at any one time, or maybe I have found a bug in ViewModel, or maybe I am doing something else wrong.


Cheers, Richard.


JonnyBee replied on Thursday, September 22, 2011

You should make sure to initialize all context at application startup.

The context will then be transferred to the background workers in DataPortal.

Any context that was uninitialized (non existing) will be created when addressed/items added in the background thread but NOT returned to the UI thread (and so would seem to be "cleared"),

richardp replied on Thursday, September 22, 2011

Hi Jonny, Thanks for your reply.

I understand that the ClientContext is passed only from the Client to the Server with each DataPortal call, and is not passed back. I am not adding anything to the ClientContext on the server side.

On the client side I set up the ClientContext at startup.

On the client side I verify (with an Assert) that the Context information I require is present in the Context before making the calls to the DataPortal (BeginRefresh). Then I make 2 async calls to the DataPortal.

On the server side, the Context information is sometimes present, sometimes not.

Sometimes it is present at first then it dissappears. It happens randomly.

Stepping thru code shown below with the debugger on the server side code I have literally seen the count change (drop to zero) as I step thru.

int count1 = ApplicationContext.ClientContext.Count;

int count2 = ApplicationContext.ClientContext.Count;

int count3 = ApplicationContext.ClientContext.Count;

I can only assume this is because the first call to the DataPortal returns and somehow clears the Context that the second call is using??

I should add that I am accessing the ClientContext thru a static helper class I added. It just adds a bit of logging and make the access more convenient. Do you think is it possible that this static class is causing the problem?

The code is like this -

public static class ClientContextHelper


public static void AddOrUpdateValue(string key, object value)


if (ApplicationContext.ClientContext.Contains(key))


ApplicationContext.ClientContext[key] = value;




_log.Debug("key ({0}) value ({1}) added", key, value);

ApplicationContext.ClientContext.Add(key, value);




richardp replied on Thursday, September 22, 2011

Doh! its OK Jonny I found my problem, I had some code that I thought was executing on the server and it was executing on the client.

Reading this post also helped me.


Copyright (c) Marimer LLC