Simplifying the WCF Service Connection - The WcfServiceDataProvider control

Posted Thu Mar 4, 2010 @ 7:25 AM

WcfServiceDataProvider is a handy new control introduced to the Silverlight and WPF suite of controls in the 2010.1 release.  The control is somewhat different from most of the other controls.  It has no visual aspect to it (hence the pictureless blog post) and its sole purpose is to simplify the task of connecting and retrieving data from an existing WCF Data Service and make the data available to other controls on the page.

The idea is quite simple:

  1. Instantiate the control in XAML by giving it the end-point configuration name of the WCF service, the service client's type and the method in the service used to retrieve the data.
  2. Updating the MethodParameters property will automatically call the registered method on the WCF service with the provided parameters, refreshing the data.  (Alternatively, you can just call Refresh() to update the data with the same parameters).
  3. You can bind to the WcfServiceDataProvider's Data property to get the latest data retrieved from the web-service.  (In WPF, binding directly to the WcfServiceDataProvider instance will have the same effect).

Example

The Simple Drill-Down demo makes extensive use of this control.  In the Silverlight version, the WcfServiceDataProvider is instantiated like this:

<svc:SalesDataServiceClient x:Key="SalesDataServiceClient" />
<CAdata:WcfServiceDataProvider IsInitialLoadEnabled="False" x:Key="Service1" 
        EndPointConfigurationName="CustomBinding_SalesDataService" 
        MethodName="GetAllData" 
        ObjectTypeRef="{StaticResource SalesDataServiceClient}" />

The easiest way to look up the Service Client type is to use intellisense in a C# code file.  Find the type by typing in the WebService class followed by a "." to look for the client type definition in the provided options.  Then instantiate the client giving it a type, and use this reference to set the ObjectTypeRef property in the control.  The EndPointConfigurationName can be looked up in the ServiceReferences.ClientConfig file in the project.

In WPF, the service client type can be looked up in the same way, but we can specify it directly in the control.  The code will look like this:

<CAdata:WcfServiceDataProvider IsInitialLoadEnabled="False" x:Key="Service1" 
        EndPointConfigurationName="BasicHttpBinding_SalesDataService" 
        MethodName="GetAllData" 
        ObjectType="{x:Type svc:SalesDataServiceClient}" />

The EndPointConfigurationName can be looked up in the app.config file in the project.

The only thing that's needed to be done in the code behind is set the Remote address of the WCF Service.  Then we can finally set some parameters and issue the first refresh call to get our data.  This should be done in the Loaded handler of the UserControl:

string remoteAddress = new Uri(Application.Current.Host.Source, "../SalesDataService.svc").AbsoluteUri;

WcfServiceDataProvider dp = this.Resources["Service1"] as WcfServiceDataProvider;
dp.DeferRefresh();
dp.RemoteAddress = remoteAddress;
dp.MethodParameters.Clear();
dp.MethodParameters.Add(new DateTime(2009, 1, 1));
dp.MethodParameters.Add(new DateTime(2010, 1, 1));
dp.Refresh();

Finally, to make use of this data, we can bind a control to the Data property of the WcfServiceDataProvider instance.  The code below binds a Chart control to the retrieved data:

<Chart:Chart x:Name="Chart1" ChartKind="Rectangle" XValue="Date"  
             DataSource="{Binding Data, Source={StaticResource Service1}}"
             ...>

</Chart:Chart>

This will cause the chart to auto update each time the WcfServiceDataProvider refreshes the data.

 

Posted to Filip Karadzic by filipK

Posted on Thu Mar 4, 2010 @ 7:25 AM

Comments

There are currently no comments for this blog post.

Anonymous comments are not allowed. Click here to log in or create an account.