Last night we were *supposed* to watch Monsters vs. Aliens but unfortunately those plans went out the window once I realized I was missing a credit card. I managed to find it eventually but since we had missed the movie I figured I’d spend some time playing around with Silverlight 3 and WCF to see if there were any big changes in the beta (other than the obvious new RIA Services). I’ve used WCF in past silverlight projects and for the most part there were no real changes in Silverlight 3. A quick rundown of the steps involved:
- Add a new Silverlight project
- Add a new Linq to Sql class (Add New Item – Linq to Sql Classes) and add a table from an existing database to the designer (e.g. Orders from Northwind)
- Add a new WCF Service to the web project and modify your service implemntation to return a list of orders. (Add New Item – WCF Service). Note that this is a standard WCF service and not a Silverlight-enabled WCF Service.
- Modify the endpoint element in your web.config so that it used basicHttpBinding instead of wsHttpBinding (Silverlight only supports basic binding)
- Add a datagrid to your MainPage.xaml
- Add a Service Reference to the Silverlight project for brevity’s sake. Ensure the ServiceReferences.ClientConfig has the proper values for the endpoint (especially the address and contract attributes). David Betz has a good overview of WCF and assembly reuse in Silverlight.
- Modify the MainPage.xaml.cs to load the appropriate data on the load event. For example:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client svc = new SL2WCF.ServiceReference1.Service1Client();
svc.GetAllOrdersCompleted += (object aSender, SL2WCF.ServiceReference1.GetAllOrdersCompletedEventArgs args) =>
{
dgMain.ItemsSource = args.Result;
};
svc.GetAllOrdersAsync();
}
No real issues compared to SL 2, but a few gotchas you might run into….
1. When a new service reference is added to a Silverlight 3 project, a ServiceReferences.ClientConfig file is automatically created. However, it seems to set the default contract attribute on the endpoint element to the wrong value. When you run the project for the first time after adding a reference, you’ll likely get the following error:
If you look at the config file you’ll see contract=”ServiceReference1.IService1″. This needs to be a fully qualified namespace, and should be changed to contract=”ProjectName.ServiceReference1.IService1″ (or whatever namespace is specified in your Reference.cs file)
2. If you’re using the dbml designer and have relationships defined between your objects, you’ll likely get an error thrown during the serialization process due to a circular reference. You may get an exception like the following:
The primary symptom of this is that your service is called properly without errors, but nothing is returned to the silverlight client. You need to do 2 things in order to resolve the circular reference
- Use DataLoadOptions with the DataContext before executing the linq command
- Set the Serialization Mode for the dbml to Unidirectional – Essentially this will add the DataContract attribute to your generated classes in ListDb.designer.cs
3. Silverlight cannot handle faults so if your Silverlight WCF Web Service call returns the following:
it could be for a number of reasons. To help debug this you can use Web Development Helper or turn on tracing