Data for Silverlight
Use the Web Service to Get the Data
The Sample Application > Implement the Client Side > Use the Web Service to Get the Data

We are finally ready to get the data and show it to the users. Complete the following:

  1. Open the MainPage.xaml.cs file and add the following using statements to the block at the top of the file:
    C#
    Copy Code
    using System.IO;
    using C1.Silverlight.Data;
    using MasterDetail.DataService;
    
  2. Next, edit the page constructor as follows:
    C#
    Copy Code
    public Page() 
    { 
        InitializeComponent(); 
        LoadData(); 
    }
    
  3. Add the following code to implement the LoadData method to invoke the Web service to retrieve the data into a DataSet object, which we bind to the controls on the page:
    C#
    Copy Code
    DataSet _ds = null;
    void LoadData()
    {
      // Invoke Web service
      var svc = GetDataService();
      svc.GetDataCompleted += svc_GetDataCompleted;
      svc.GetDataAsync("Categories,Products");
    }
    void svc_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
    {
      // Handle errors
      if (e.Error != null)
      {
        _tbStatus.Text = "Error downloading data...";
        return;
      }
     
      // Parse data stream from server (DataSet as XML)
      _tbStatus.Text = string.Format(
        "Got data, {0:n0} kBytes", e.Result.Length / 1024);
      var ms = new MemoryStream(e.Result);
      _ds = new DataSet();
      _ds.ReadXml(ms);
     
      // Got the data, bind controls to it
      BindData();
    }
    
  4. Add the following GetDataService method implementation:
    C#
    Copy Code
    // Get data service relative to current host/domain
    DataServiceSoapClient GetDataService()
    {
      // Increase buffer size
      var binding = new System.ServiceModel.BasicHttpBinding();
      binding.MaxReceivedMessageSize = 2147483647; // int.MaxValue
      binding.MaxBufferSize = 2147483647; // int.MaxValue
     
      // Get absolute service addres
      Uri uri = GetAbsoluteUri("DataService.asmx");
      var address = new System.ServiceModel.EndpointAddress(uri);
     
      // Return new service client
      return new DataServiceSoapClient(binding, address);
    }
    public static Uri GetAbsoluteUri(string relativeUri)
    {
      Uri uri = System.Windows.Browser.HtmlPage.Document.DocumentUri;
      string uriString = uri.AbsoluteUri;
      int ls = uriString.LastIndexOf('/');
      return new Uri(uriString.Substring(0, ls + 1) + relativeUri);
    }
    

The GetDataService method instantiates and returns a new DataServiceSoapClient object. We don't use the default constructor because that would refer to the development environment (http://localhost and so on). It would work correctly on the development machine, but would break when the application is deployed. Also, the default constructor uses a 65k buffer that might be too small for our data transfers. The above GetDataService method implementation takes care of both issues.

The LoadData method above instantiates the service and invokes the GetDataAsync method. When the method finishes executing, it invokes the svc_DataCompleted delegate. The delegate instantiates a DataSet object, uses the ReadXml method to de-serialize the data provided by the server, then calls BindData to bind the data to the controls on the page.

Note: This is one of the most important features of the C1.Silverlight.Data DataSet class. It uses the same XML schema that ADO.NET DataSet objects use. This allows applications to serialize data on the client and de-serialize it on the server, or vice-versa. The fact that the object models are very similar also makes things substantially easier for the developer.

See Also