January 20, 2021
Hot Topics:

Getting Started with Microsoft Synchronization Services for ADO.NET

  • By Rachel Baker
  • Send Email »
  • More Articles »

The Server Web Service

Your web service will be responsible for retrieving information from your database to push to the clients as well as submitting information from the clients back to the server database. To get started, create a new C# ASP.NET Web Service project in Visual Studio:

Click here for a larger image.

Figure 2: New C# ASP.NET Web Service project in Visual Studio 2005

I called my web service "SyncServicesSample_WebService." You will first need to add references to the two sync services libraries that deal with server-side sync logic. They are Microsoft.Synchronization.Data.dll and Microsoft.Synchronization.Data.Server.dll, and they can be found at "C:\Program Files\Microsoft Synchronization Services\ADO.NET\v1.0." Secondly, remove the HelloWorld default web method provided by Visual Studio. I also renamed my Service class, asmx file, and CS file to SyncServicesSampleService.

Next, open web.config and add a connection string pointing to the database you just set up in SQL Server:

   <add name="SampleServerDB" connectionString="
        Data Source=localhost;
        Initial Catalog=SyncServicesSample_ServerDB;
        Integrated Security=True"/>

This is also a good time to make sure your web service is configured for Windows integrated security. To do so, make sure the authentication mode is set to Windows and the identity impersonation is set to true. The authentication section of your web.config should like this:

<authentication mode="Windows" />
<identity impersonate="true"/>

Finally, you are ready to start coding the meat of the web service. The web service is going to serve as a wrapper class for the Microsoft.Synchronization.Data.Server.DbServerSyncProvider class, which is responsible for updating and retrieving data from the server database./p>

private DbServerSyncProvider serverSyncProvider;

To implement a server sync provider web service, you must implement four methods that sync services uses to do the work of the synchronization. Your methods will simply pass the call on to your serverSyncProvider object. The methods are as follows:

Listing 1: Server sync provider web service methods

public SyncServerInfo GetServerInfo(SyncSession session)
   return serverSyncProvider.GetServerInfo(session);

public SyncSchema GetSchema(Collection<string> tableNames,
   SyncSession session)
   return serverSyncProvider.GetSchema(tableNames, session);

public SyncContext GetChanges(SyncGroupMetadata groupMetadata,
   SyncSession syncSession)
   return serverSyncProvider.GetChanges(groupMetadata,

public SyncContext ApplyChanges(SyncGroupMetadata groupMetadata,
   DataSet dataSet, SyncSession syncSession)
   return serverSyncProvider.ApplyChanges(groupMetadata, dataSet,

In the constructor, you will initialize the serverSyncProvider variable and set its connection property to the location of your database. I also like to add an ApplyChangeFailed event handler. Visual Studio will generate the handler for you, and you can add any logging/failure logic you would like. I like to put a breakpoint on the handler while debugging so I can tell what is going wrong. Here is the code:

Listing 2: Server sync provider initial constructor code

serverSyncProvider = new DbServerSyncProvider();

serverSyncProvider.ApplyChangeFailed += new

SqlConnection serverConn = new
serverSyncProvider.Connection = serverConn;

Page 2 of 8

This article was originally published on December 17, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date