views:

14

answers:

1

I have a Silverlight 4 application that connects to an OData service. My model is submitting multiple queries to the service in parallel and in the query callback I am processing the results using a backgroundworker.

This works great if I am making one call, but as soon as I make more than one call in parallel - sometimes it works - but most of the time I get an exception in the dataservice context when I call EndExecute (it appears to be when it is adding entities to its internal change tracking mechanism.)

So I decided to lock the data service at the point I call EndExecute

lock (dataService)
{
    results = query.EndExecute(queryCallback).ToList<Video>(); 
}

Which fixed the problem and it now reliably processes the data requests.

My question twofold - is this a recomended practice? Is the OData context not thread safe?

+1  A: 

DataServiceContext is not thread-safe. It doesn't have any thread affinity, but it doesn't support having more than one thread calling into it concurrently.

So yes, you have to ensure that only one thread enters the context and associated objects (e.g. query objects) at a time by using a lock or some other means.

Pablo Castro
Could I create multiple instances of the odata service as an alternative to locking a single instance? Pros/cons?
MIantosca