Fix Unhandled Error in Silverlight 2 Application Invalid cross-thread access error

Had a support call from a client today regarding a problem them were running into with Client OM access from a Silverlight application. The exact error was “Invalid cross-thread access”. Sitting down with the developer at the site I determined that this problem can be caused by two issues:

1) Your XAP is not copied into the right location. It is supposed to go into the 14 hive ClientBin directory under LAYOUTS. If the XAP is not present, this error can occur.

2) Objects in code and not being loaded correctly (this turned out to be the primary issue for my particular problem). Consider the following simply ListItem update code:

private void UpdateData()
ClientContext clientContext = new ClientContext(“url”);
Web web = clientContext.Web;
ListCollection listCollection = web.Lists;
list = listCollection.GetByTitle(“TestList”);
ListItem li = list.GetItemById(1);
li[“Title”] = “My new item”;
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);

private void onQuerySucceeded (object sender, ClientRequestSucceededEventArgs args)
// I am not displaying show ShowMessage, but assume that it writes the list title to standard output
UpdateUIMethod updateUI = ShowMessage;

private void onQueryFailed (object sender, ClientRequestFailedEventArgs args)

Now, looking back at the error, what’s missing in the above code is the load! The objects will not be initialized with this code, which can cause that particular error.
To fix it, include the load, such as (fully qualified instantiation demoed, you would obviously use your own object conventions):
list => list.Title);
before executing the query, and the error should go away!


Leave a Reply

Your email address will not be published. Required fields are marked *