Silverlight
WCF Data Services
WebGet
Async Programming
Service Consumption

Consuming WCF Data Services Service Operator WebGet async from Silverlight

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Silverlight, remote service calls must be asynchronous so the UI remains responsive. If a WCF Data Services endpoint exposes a service operation with WebGet, the usual client approach is to build the operation URI and execute it asynchronously through DataServiceContext rather than trying to treat it like a local blocking method.

What the Service Operation Represents

In older WCF Data Services applications, a service operation could expose custom server logic through an addressable URI. If the operation used WebGet, the client would call it over HTTP GET.

A server-side example looked roughly like this:

csharp
1[WebGet]
2public IQueryable<Product> FeaturedProducts()
3{
4    return this.CurrentDataSource.Products.Where(p => p.IsFeatured);
5}

From the client point of view, the important detail is that the operation becomes a URI under the service root.

Why the Silverlight Client Must Be Async

Silverlight does not allow long-running network work to behave like ordinary synchronous desktop code. Blocking the UI thread is not acceptable, so the service call must start asynchronously and complete through a callback.

That makes the shape of the problem clear:

  • build the correct operation URI
  • execute it asynchronously
  • read the result in the callback
  • marshal any UI updates back onto the UI thread

Using BeginExecute and EndExecute

For a generated WCF Data Services client, the standard legacy pattern is BeginExecute with EndExecute.

csharp
1using System;
2using System.Data.Services.Client;
3using System.Linq;
4using System.Windows;
5
6Uri serviceRoot = new Uri("http://example.com/ProductService.svc/");
7var context = new ProductEntities(serviceRoot);
8Uri requestUri = new Uri(serviceRoot, "FeaturedProducts");
9
10context.BeginExecute<Product>(requestUri, ar =>
11{
12    try
13    {
14        var response = context.EndExecute<Product>(ar);
15        var products = response.ToList();
16
17        Deployment.Current.Dispatcher.BeginInvoke(() =>
18        {
19            ProductsListBox.ItemsSource = products;
20        });
21    }
22    catch (DataServiceQueryException ex)
23    {
24        Deployment.Current.Dispatcher.BeginInvoke(() =>
25        {
26            ErrorTextBlock.Text = ex.Message;
27        });
28    }
29}, null);

This does three important things:

  • the network call happens asynchronously
  • the response is materialized in the callback with EndExecute
  • UI changes are pushed through the dispatcher

Handling Parameters

If the service operation takes parameters, they are expressed in the operation URI. Build that URI carefully.

csharp
1string category = "Books";
2Uri requestUri = new Uri(
3    serviceRoot,
4    "FeaturedProductsByCategory?category='" + category + "'"
5);

With legacy OData-style endpoints, small quoting mistakes can easily produce confusing errors. Parameter encoding deserves care, especially when values contain spaces or punctuation.

Separating Network Logic from UI Logic

Legacy Silverlight code often becomes hard to read because fetching data, parsing it, handling errors, and updating controls all happen in one anonymous callback. Even if you keep the same async API, it helps to think in stages:

  • send the request
  • complete the request and materialize the response
  • handle service errors
  • update controls on the dispatcher

That structure makes the code easier to debug.

When a Raw HTTP Client Is Easier

Sometimes the generated data-service client does not expose the service operation nicely, or the endpoint behaves more like a custom REST call than an entity query. In those cases, using WebClient and parsing the response manually can be more practical.

Still, if you already have a DataServiceContext, BeginExecute is usually the most natural first choice.

Common Pitfalls

A common mistake is updating Silverlight controls directly from the async callback without using the dispatcher. That causes thread-affinity problems.

Another mistake is constructing the wrong URI for the service operation. Service operations are not always shaped like ordinary entity-set queries, so verify the exact path.

A third issue is thinking that because the operation uses WebGet, it can be called like a cheap local method. It is still a remote HTTP request and should be treated like one.

Summary

  • A WebGet service operation is a GET-based endpoint exposed by WCF Data Services
  • Silverlight clients must consume it asynchronously
  • 'DataServiceContext.BeginExecute and EndExecute are the normal legacy client pattern'
  • Use the dispatcher when updating UI controls from the callback
  • Build operation URIs carefully, especially when parameters are involved

Course illustration
Course illustration

All Rights Reserved.