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
  • Saturday, December 22, 2007

    Integrated WiX support in SharpDevelop


    In fact, WiX is the Open source toolset which generates Windows Installation packages or MSI files. WiX provides command line tools to build MSI packages.


    Wikipedia describes the WiX toolset as follows:

    The Windows Installer XML toolset (WiX), pronounced "wicks", is a free, open-source toolset that builds Windows Installer (MSI) packages from an XML document. It supports a command-line environment that developers may integrate into their build processes to build MSI and MSM setup packages. ...

    The developer has to create an xml file with .Wxs extension. This file contains all files, folders, registry keys, SQL databases, IIS directories, users, shortcuts, … which must be included in the installation. Creating the .Wxs file is not that easy. Currently, there is no user-friendly application to create this file. There is already an Open Source project, called WixGUI, but they haven’t released anything yet ...


    Compilation process


    To build our MSI file with WiX, we need 2 executables: Candle.exe & Light.exe.
    1.Candle compiles the .Wxs file into a .WixObj file. (intermediary xml file)
    2.Light transforms the .WixObj into the final MSI package

    You can create your own batch file with following commands:
    candle Test.wxs
    light.exe -out Test.msi Test.wixobj

    Sharp Developer



    Did you ever heard about SharpDevelop? SharpDevelop is the Open Source Integrated Development Environment in contrast to Visual Studio IDE. SharpDevelop has also integrated WiX support. You can visually create your .Wxs file. You can also design custom installation dialogs and watching the result before executing the installation package. By pressing F5 (alias ‘Build solution’) the Candle and Light executables are automatically executed. So by building your solution, your MSI file will be created automatically!

    SharpDevelop has Setup projects which has built-in WiX support:


    In the Setup project you have a tree-structure with all files, directories, components, ... which can be added visually.


    You can also view the resulting customized installation dialogs, without executing the installation package:



    Remarks


    There is also a project, called Votive, which provides WiX support for Visual Studio .NET/2005/2008. This integration allows you to have intelliSense in the Wxs file, but you cannot create files, directories, etc… visually!


    References


    SharpDevelop Community
    WiX online tutorials
    Article on SharpDevelop and integrated WiX support