Saturday, June 15, 2013

Create sample application using Microsoft sync framework

The Sync Framework ships with the following three out-of-the-box synchronization providers.

·         Synchronization provider for files and folders.
·         Synchronization provider for ADO.NET-enabled data sources
·          Synchronization provider for RSS and Atom feeds


Using the built-in sync providers is very easy. The tasks can be summarized as follows:

1. Create unique Globally Unique Identifiers (GUIDs) for the source replica and
destination replica.
2. Create a source provider by creating a new instance of the built-in provider and
attaching the source provider to the source replica.
3. Create a destination provider by creating a new instance of the built-in provider
and attaching the destination provider to the destination replica.
4. Create a new instance of a sync agent and attach the source and destination
provider to it.
5. Set the direction of the synchronization by using the sync agent. The sync application
can now use the sync agent to start synchronization.

Let’s now dig into some code. Recall that the file sync provider helps to synchronize the
files, folders, and subfolders.

1.    Create a new Windows Forms application using Visual Studio 2010 and name it
SyncApp_BuiltInFileProviders as shown below screen shot.



2.    Add a reference to Microsoft.Synchronization and Microsoft.Synchronization.
Files. (The Add Reference dialog box shown in below can be launched by
right-clicking the project in Solution Explorer and clicking Add a Reference.)


3.    Place a button on form1 and name it btnSynchronize with this text: Synchronize.
Double-click the button to wire an event handler for the button.

4.  Place a label on the form with its Name as label1 and text as Click on Synchronize
button to start the synchronization.

5. Add two folders in your C drive and name them Image1 and Image2. Create a
new texts file in Image1 and name it Image1.txt. Open the file and type some
text into it.

6.In the using block at the top of the form1.cs, add the following two namespaces:

using Microsoft.Synchronization;
using Microsoft.Synchronization.Files;

7. Create two new GUIDs for the source and destination replicas, as shown in the
following code:

        Guid sourceReplicaId;
        Guid destReplicaId;
        private void Form1_Load(object sender, EventArgs e)
        {
            //Assign Unique Guid's to the Replicas
            sourceReplicaId = Guid.NewGuid();
            destReplicaId = Guid.NewGuid();
        }

8. Write the following code in the btnSynchronize_Click event (the main logic of the
code resides in this event):

       private void btnSynchronize_Click(object sender, EventArgs e)
        {

          btnSynchronize.Enabled = False;
            //Create the Source and destination Sync provider.
            //Attach the source sync provider to C:\Image1 folder and
            //assign it a unique replica guid.
            FileSyncProvider sourceProvider = new FileSyncProvider(sourceReplicaId,
            @"C:\ Image1 ");
            //Attach the destination sync provider to C:\ Image2 folder and
            //assign it a a unique replica guid.
            FileSyncProvider destProvider = new
            FileSyncProvider(destReplicaId, @"C:\ Image2");
            //syncAgent is the Sync Controller and it co-ordinates the sync session
            SyncOrchestrator syncAgent = new SyncOrchestrator();
            syncAgent.LocalProvider = sourceProvider;
            syncAgent.RemoteProvider = destProvider;
            syncAgent.Synchronize();
            label1.Text = "Synchronizing Finished...";
            btnSynchronize.Enabled = True;

        }


The code is incredibly simple. We created the source and destination sync providers
by passing the replica IDs and the folder name as a parameter. Then we
created a new instance of SyncOrchestrator (also known as the sync agent) and

attached the two providers to it. Finally we called the Synchronize method on it.
That’s it; we wrote the code to synchronize two replicas (Image1 and Image2)
using two instances of a built-in file sync provider.

Run the application and click the Synchronize button.



No comments:

Post a Comment