Microsoft Sync Framework

I recently had a requirement to move backups from a server’s local filesystem over to a network location now my first thoughts were of a windows service and a FileSystemWatcher watching the local directory and upon a folder/file being changed copy the file over to the network, however once you start digging into the details you find it isn’t as straight forward for one thing if a backup file is deleted on the server to free up space (ony the last month’s worth for example) this should be reflected on the network end plus there’s also file renames, locked files etc…

Anyhow I googled around and found the MS Sync Framework which is specially created to deal with these scenario’s and out of the box comes with providers for dealing with file synchronization great stuff! The framework is pretty big as it has extension points to let you do stuff with custom providers (for other transport synchronization), custom data transformation, metadata stored items etc… however for what I wanted I barely needed to scratch the surface.

In it’s most basic form you end up with the following code:

FileSyncProvider sourceProvider = null ;
FileSyncProvider destinationProvider = null ;

    sourceProvider = new FileSyncProvider(
        sourceReplicaId, sourceReplicaRootPath, filter, options);
    destinationProvider = new FileSyncProvider(
        destinationReplicaId, destinationReplicaRootPath, filter, options);

    destinationProvider.AppliedChange +=
        new EventHandler (OnAppliedChange);
    destinationProvider.SkippedChange +=
        new EventHandler (OnSkippedChange);

    SyncOrchestrator orchestrator = new SyncOrchestrator ();
    orchestrator.LocalProvider = sourceProvider;
    orchestrator.RemoteProvider = destinationProvider;
    orchestrator.Direction = SyncDirection.Upload; // Sync source to destination

    Console .WriteLine( "Synchronizing changes to replica: " +
    // Release resources
    if (sourceProvider != null ) sourceProvider.Dispose();
    if (destinationProvider != null ) destinationProvider.Dispose();

There is one gotcha that caught me out, the MS Sync Framework will not do any of the scheduling of when the synchronization is performed that is completely dealt with by you, so in my case I was back to using a FileSystemWatcher to watch for file changes in the source folder and once a change was detected I would then tell the SyncOrchestrator to synchronize this seemed to work fine and was not that much extra work. The only other step was to wrap this functionality up to be used from a windows service and that was it! Overall I would recommend anyone looking to perform synchronization to take a look to see if either there’s something out of the box the framework gives you or if it can give you a head start.


2 thoughts on “Microsoft Sync Framework

  1. any sample code of all solution for do backups (localsystem – network location, please ? thx, happy new year

  2. @espinete, sorry no can do that code was for the company I work for, but there are a few examples like the above lying around, good luck have a good new year!

Comments are closed.