I posted a few months back about Phani’s OData async extension methods that convert the begin/end async pattern of the WCF Data Services client library into methods that support the new (and much easier) async/await pattern introduced in .NET Framework 4.5. I also added some critical missing async extension methods (such as LoadAsync) in my post Extending the OData Async Extensions to DataServiceCollection<T> Methods. I was reminded by a reader that in this post I neglected to actually show how to use these extension methods in code. This didn’t occur to me because the point of the LoadAsync extension method was to be able to call this method with await, but since the LoadAsync pattern itself it a little tricky (I frequently forget it myself), I figured why not call it out one more time….
For example, here's how you would load the Customers feed from the ubiquitous Northwind data service using the new C# await keyword using the async extension for LoadAsync:
// Create a LINQ query that returns customers with related orders.
var customerQuery = from cust in _context.Customers
where cust.Country == customerCountry
// Create a new collection for binding.
var _customers = new DataServiceCollection<Customer>(context);
// Load products.
// Execute the query and get the first page of results.
while (_customers.Continuation != null)
// Load all remaining pages of the response.
catch (DataServiceQueryException ex)
this.Message = "The query could not be completed:\n" + ex.ToString();
Note that this example calls LoadAsync once to load the first page of data, and then it continues to call LoadAsync if there are additional pages in the response. LoadAsync is the trickiest method to use correctly, because it’s so overloaded (in a good way). Let me know if you also have any trouble with ExecuteAsync, LoadPropertyAsync or those ones.