Sync Services for Devices Working Sample Download

There is a walkthrough here on how to create a Sync Services for Devices application on MSDN but it doesn’t quite work, at least I had to modify it to make things work. 

Note: You can download the Northwind database here.  Best bet with SQL Server 2008 is to open and run the script, not attach the database files.

There are some comments at the bottom that can help but it is still incomplete/not clear so I am posting some tips here that I hope will show how easy it really is to get things working, once you know what to do:-)

Pretty much you can follow all of the steps in the walkthrough to get mostly completed.  Where I had to deviate is primarily in two areas:

  • Updating the web reference proxy after adding the web reference
    • These steps also have to be done if you update the IP address for the web service call if  the local machine’s IP address changes
  • Synchronization code

To get started, follow the walkthrough at the above link until about the step titled “Adding a Web reference from the project to the WCF service”.  Complete that step but also perform the actions below.

Additional steps for the section titled “Adding a Web reference from the project to the WCF service”

In addition to removing all classes except the first class in reference.cs and adding the two using statements per the walkthrough instructions:

using Microsoft.Synchronization.Data; using Microsoft.Synchronization;

I also had to remove the three generated datasets located under Reference.map

  • SyncContext.datasource
  • SyncSchema.datasource
  • SyncServerInfo.datasource

Note: if you move your laptop between networks such that the IP of your desktop changes, you will need to do a search and replace of your old IP with the new IP. This will auto-generate reference.cs as well as the three datasets above so you will have to repeat this step.

Next add code to perform the synchronization.

Code to synchronize the data between the device .sdf database and server database

This code would be placed in the “Synchronize Now” button event handler.  First, I added a using statement at the top of frmMain to bring in the web service reference proxy class namespace:

using NorthwindMobile.NorthwindDataCacheSyncServiceRef;

The sample attached to this blog post has additional code around it but here are the important lines of code required to perform the synchronization.

NorthwindDataCacheSyncAgent _syncAgent = new NorthwindDataCacheSyncAgent(new NorthwindDataCacheSyncService());

_syncAgent.Synchronize();

The class NorthwindDataCacheSyncService is located in the NorthwindMobile.NorthwindDataCacheSyncServiceRef and is the class for the web service.

The NorthwindDataCacheSyncAgent is also autogenerated when you add sync services to the mobile project and is located in theNorthwindDataCache.Client.Sync.Designer.cs file. 

When you add the database cache project and the web reference to the middle-tier, there are auto-generated names for various objects required to perform synchronization. So calling the sync code is a matter of running down the auto-generated names for the above to objects.  The attached sample should help you understand what’s going on. 

Other Functionality

In addition to the above changes, the attached code deviates somewhat from the walkthrough when creating a data-bound form, but I think in a somewhat useful way.  I added a DataGrid to the frmMain object and set its DataSource property to the customers table located in the generated dataset called NorthwindDataSet.xsd.  The generated dataset was created as part of adding the local database cache components to the Smart Device application.

If you select the DataGrid in the forms designer, click the arrow for design-time options, and then click “Generate Data Forms…” it will generate a view and edit data form for the data displayed in the grid, making for an easy way to test out editing data and verifying updates are happening after synchronization.

The one important additional step is to show is how to persist changes made to the NorthwindDataSet.xsd back to the Northwind.sdf CE database on the device:

private void menuItemSaveChanges_Click(object sender, EventArgs e)
{
   customersTableAdapter.Update(northwindDataSet);
   northwindDataSet.AcceptChanges();
   menuItemSaveChanges.Enabled = false;
}

You must call Update on the auto-generated TableAdapter class for the Customers table before calling AcceptChanges.  Otherwise the changes will not persist to the local CE database and therefore will not be synchronized back to the server database.

When you successfully run the application, the WCF Test Client should look like this:

image

When the application runs, click on a record to go to the View / Edit Page:

 

image image

SyncServicesV1Sample.zip