October 26, 2016
Hot Topics:

Getting Started with Microsoft Synchronization Services for ADO.NET

  • December 17, 2007
  • By Rachel Baker
  • Send Email »
  • More Articles »

Like the ServerSyncProvider, you need to tell the ClientSyncProvider in the constructor how to talk to the client database. We will use our value from the app.config. The only other thing we will do in the constructor is to hook up to "schema events." These will allow us to define settings, such as the rowguid property, which are not copied down when a client database is created from a server database. The complete constructor looks like the following:

Listing 11: Client sync provider constructor code

public SampleClientSyncProvider()
   this.ConnectionString =

   this.CreatingSchema += new
   this.SchemaCreated += new

You will add code to the CreatingSchema and SchemaCreated event handlers for the bi-directional sync tables to ensure that proper primary keys are generated when rows are inserted. This is accomplished like so:

Listing 12: Client sync provider event handler code

void SampleClientSyncProvider_CreatingSchema(object sender,
   Microsoft.Synchronization.Data.CreatingSchemaEventArgs e)
   switch (e.Table.TableName)
      case "Customer":
            .RowGuid = true;

      case "CustomerFavoriteItem":
            .Columns["CustomerFavItemID"].RowGuid = true;

void SampleClientSyncProvider_SchemaCreated(object sender,
   Microsoft.Synchronization.Data.SchemaCreatedEventArgs e)
   MakeSchemaChangesOnClient(e.Connection, e.Transaction,

void MakeSchemaChangesOnClient(IDbConnection clientConn,
   IDbTransaction clientTran, string tableName)
   SqlCeCommand alterTable = new SqlCeCommand();
   alterTable.Connection  = (SqlCeConnection)clientConn;
   alterTable.Transaction = (SqlCeTransaction)clientTran;
   alterTable.CommandText = String.Empty;

   switch (tableName)
      case "Customer":
         alterTable.CommandText =
         "ALTER TABLE [Customer] " +
         "ADD CONSTRAINT DF_CustomerID " +
         "DEFAULT NEWID() FOR CustomerID";

      case "CustomerFavoriteItem":
         alterTable.CommandText =
         "ALTER TABLE [CustomerFavoriteItem] " +
         "ADD CONSTRAINT DF_CustomerFavItemID " +
         "DEFAULT NEWID() FOR CustomerFavItemID";

As you can see from the code, you are just instructing SQL Server CE to set the RowGuid property on the primary keys of the Customer and CustomerFavoriteItem tables and to set the default value of those fields to newid(). The ClientSyncProvider is now finished.

It is time to tie it all together with the SyncAgent. Create a new class (for example, SampleSyncAgent). This class will extend the SyncAgent class and requires the following using directives:

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

As I mentioned earlier, the SyncAgent will hold the references to the ClientSyncProvider (LocalProvider) and the ServerSyncProvider (RemoteProvider). The LocalProvider is easy:

this.LocalProvider = new SampleClientSyncProvider();

Because you implemented the ServerSyncProvider as a web service, it will require a bit more work. I must warn you, this part gets a bit tricky. You need to add a web reference to your web service just as you would with any project that consumes a web service. Visual Studio will generate a Reference.cs file that will attempt to create types for the web service-exposed objects instead of using the ones defined in the sync services libraries. For this reason, you will have to manually edit the references file to include the actual sync services types. First, locate the references file. It will be located in your project's folder in the Web References folder. Open the file for editing (in Notepad or VS, whichever you prefer).

The first modification is to add the Microsoft.Synchronization.Data directive like so:

namespace SyncServicesSample_Client.SyncServicesSampleService {
   using  System.Diagnostics;
   using  System.Web.Services;
   using  System.ComponentModel;
   using  System.Web.Services.Protocols;
   using  System;
   using  System.Xml.Serialization;
   using  System.Data;
   using  Microsoft.Synchronization.Data;

Page 6 of 8

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

Sitemap | Contact Us

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