Starting with the Synchronization Framework

If you don't already know, the Synchronization Framework team has just released the CTP for v2.0

There are 3 core areas of the framework:-

  • Sync Services for ADO.NET: Synchronization for ADO.NET enabled data sources
  • Sync Services for File Systems: Synchronization for files and folders
  • Sync Services for FeedSync: Synchronization for RSS and ATOM feeds

The framework home page contains links to lots of decent resources. The introduction to the framework, discusses file synchronization between two directories. This for me was a great place to start. As the document was written in November 2007, there was bound to be a few changes as to how you call various methods. I will cover here some of those changes for anybody who is interested.

   1: // Set options for the sync operation
   2: FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletes | FileSyncOptions.RecycleOverwrites;

Becomes

   1: // Set options for the sync operation
   2: FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecycleConflictLoserFiles;
   1: SyncAgent agent = new SyncAgent();
   2: agent.LocalProvider = sourceProvider;
   3: agent.RemoteProvider = destinationProvider;
   4: agent.Direction = SyncDirection.Upload; // Sync source to destination
   5: Console .WriteLine( "Synchronizing changes to replica: " + destinationProvider.RootDirectoryPath);
   6: agent.Synchronize();

Becomes

   1: SyncOrchestrator agent = new SyncOrchestrator();
   2: agent.LocalProvider = sourceProvider;
   3: agent.RemoteProvider = destinationProvider;
   4: agent.Direction = SyncDirectionOrder.Upload; // Sync source to destination
   5: Console.WriteLine("Synchronizing changes to replica: " + destinationProvider.RootDirectoryPath);
   6: agent.Synchronize();
   1: switch (args.ChangeType)
   2:             {
   3:                 case ChangeType.Create:
   4:                     Console .WriteLine( "-- Applied CREATE for file " + args.NewFilePath);
   5:                     break ;
   6:                 case ChangeType.Delete:
   7:                     Console .WriteLine( "-- Applied DELETE for file " + args.OldFilePath);
   8:                     break ;
   9:                 case ChangeType.Overwrite:
  10:                     Console .WriteLine( "-- Applied OVERWRITE for file " + args.OldFilePath);
  11:                     break ;
  12:                 case ChangeType.Rename:
  13:                     Console .WriteLine( "-- Applied RENAME for file " + args.OldFilePath +
  14:                                       " as " + args.NewFilePath);
  15:                     break ;
  16:             }

Becomes

   1: switch (args.ChangeType)
   2:             {
   3:                 case ChangeType.Create:
   4:                     Console.WriteLine("-- Applied CREATE for file " + args.NewFilePath);
   5:                     break;
   6:                 case ChangeType.Delete:
   7:                     Console.WriteLine("-- Applied DELETE for file " + args.OldFilePath);
   8:                     break;
   9:                 case ChangeType.Update:
  10:                     Console.WriteLine("-- Applied OVERWRITE for file " + args.OldFilePath);
  11:                     break;
  12:                 case ChangeType.Rename:
  13:                     Console.WriteLine("-- Applied RENAME for file " + args.OldFilePath +
  14:                                       " as " + args.NewFilePath);
  15:                     break;
  16:             }

However the biggest change I have come across with this example is that the FileSyncProvider takes in a GUID as a parameter, not a Synchronization.SyncId

The SyncId does have a public method which returns a guid like so:-

Guid guidSourceReplicaId = sourceReplicaId.GetGuidId();

So just use this instead, what is the difference you may ask? Well a guid comprises of 5 blocks of hex separated with hyphens, and the SyncId is just the hex without any hyphen.

The whole Synchronization Framework seems such a fantastic technology I hope to delve in a bit deeper in the coming months.

1 Comment

Comments have been disabled for this content.