Wednesday, December 26, 2007

Microsoft Sync Framework CTP

Microsoft has released the Microsoft Sync Framework CTP 1 few weeks ago.
With the MS Sync Framework you can easily synchronize any application, any type of data, using any protocol over any network.

The steps below explains how you to synchronize a folder. In fact, you can use the following example to create a backup of a certain folder:

//Start method
public void Backup()
{
/*1. Create or retrieve SyncId file (a Sync Id file contains a GUID to uniquely identify each folder*/
SyncId sourceSyncId = GetSyncId(Path.Combine(sourcePath, idFileName));
SyncId destinationSyncId = GetSyncId(Path.Combine(destinationPath,idFileName));

/* Adding synchronization options */
FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges | FileSyncOptions.RecycleDeletes;

/* Adding synchronization filters: do not synchronize/backup the SyncId file.*/
FileSyncScopeFilter filter = new FileSyncScopeFilter();
filter.FileNameExcludes.Add(idFileName); // Exclude the generated SyncId file

/* Detect synchronization changes.*/
DetectChanges(sourceSyncId, sourcePath, filter, options);
DetectChanges(destinationSyncId, destinationPath, filter, options);

/* Synchronize (backup) source to destinationfolder*/
SyncFiles(sourceSyncId, destinationSyncId, sourcePath, destinationPath, filter, options);
}


In the Backup() method we used following supporting methods:

private void DetectChanges(SyncId syncId, string path, FileSyncScopeFilter filter, FileSyncOptions options)
{

using (FileSyncProvider provider = new FileSyncProvider(syncId, path, filter, options))
{
provider.DetectChanges();
}
}

private void SyncFiles(SyncId sourceSyncId, SyncId destinationSyncId, string sourcePath, string destinationPath, FileSyncScopeFilter filter, FileSyncOptions options)
{

using (FileSyncProvider sourceProvider = new FileSyncProvider(sourceSyncId, sourcePath, filter, options))
using (FileSyncProvider destinationProvider = new FileSyncProvider(destinationSyncId, destinationPath, filter, options))
{
//Create SyncAgent
SyncAgent agent = new SyncAgent();
agent.LocalProvider = sourceProvider;
agent.RemoteProvider = destinationProvider;

//Upload = Synchronize files from LocalProvider to RemoteProvider
agent.Direction = SyncDirection.Upload;

//Synchronize!
agent.Synchronize();
}
}

private SyncId GetSyncId(string idFilePath)
{

SyncId replicaId = null;

//Try to read existing ReplicaID
if (File.Exists(idFilePath))
{
using (StreamReader sr = File.OpenText(idFilePath))
{
string strGuid = sr.ReadLine();
if (!string.IsNullOrEmpty(strGuid))
{
replicaId = new SyncId(new Guid(strGuid));
}
}
}
//If not exist, Create ReplicaID file
if (replicaId == null)
{
using (FileStream idFile = File.Open(idFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(idFile))
{
replicaId = new SyncId(Guid.NewGuid());
sw.WriteLine(replicaId.GetGuidId().ToString("D"));
}
}
}

return replicaId;
}


Resources

  • Click here to download the MS Sync Framework CTP 1.

  • Sync framework Development Center

  • Sync framework Blog
  • No comments: