none
Different location for filesync.metadata does not work? RRS feed

  • Question

  • I'm new to the sync framework, so maybe this is stupid.

    Though, I'm trying to store the metadata file in a different location than the folder being syncronized.

    This doesnt seem to work.

    I am using the third constructor of FileSyncProvider.

    In fact the strange thing is, if the source folder is indecated by sourceReplicaRootPath and I use :

    sourceProvider = new FileSyncProvider(aSourceGuid,
                        sourceReplicaRootPath, filter, options,  sourceReplicaRootPath, "filesync.metadata", tempPath, losersPath);

    (similar for target)

    then all works fine, but ofcource this is the same as if the metadatafolder and metadataname were not specified.

    Now when I - for example -  just change "filesync.metadata" into "files.metadata" and I syncronize then I get two files ("filesync.metadata" and "files.metadata") in the folder and that's it... no synchronization.

    Same happens if I change the metadatafolder. The metadatafolder gets a "files.metadata" file and the actual source folder gets a "filesync.metadata" file and again no synchronization happens.

    What am I doing wrong?

    Example

    (based on http://msdn.microsoft.com/en-us/sync/bb887623.aspx)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Synchronization;
    using Microsoft.Synchronization.Files;
    using System.IO;
    using System.Diagnostics;

    namespace CeSync
    {
        public partial class Form1 : Form
        {
            private Guid sourceGuid = Guid.NewGuid();
            private Guid targetGuid = Guid.NewGuid();

            private static String CeSyncRoot = "C:\\Users\\mvermand\\AppData\\Local\\CeSync\\";

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    String replica1RootPath = "c:\test";
                    String replica2RootPath = "c:\temp";
                    // Set options for the synchronization operation
                    FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |
                             FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates ;

                    FileSyncScopeFilter filter = new FileSyncScopeFilter();
                    filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files

                    // Synchronization in both directions
                    SyncFileSystemReplicasOneWay(sourceGuid, targetGuid,  replica1RootPath, replica2RootPath, null, options);
                    SyncFileSystemReplicasOneWay(targetGuid, sourceGuid, replica2RootPath, replica1RootPath, null, options);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("\nException from File Synchronization Provider:\n" + ex.ToString());
                    Debug.WriteLine(ex.StackTrace);
                }
            }

           

            public static void SyncFileSystemReplicasOneWay(Guid aSourceGuid, Guid aTargetGuid,
                    string sourceReplicaRootPath, string destinationReplicaRootPath,
                    FileSyncScopeFilter filter, FileSyncOptions options)
            {
                FileSyncProvider sourceProvider = null;
                FileSyncProvider destinationProvider = null;

                try
                {
                    sourceProvider = new FileSyncProvider(aSourceGuid,
                        sourceReplicaRootPath, filter, options,  sourceReplicaRootPath, "files.metadata", CeSyncRoot + "temp1", CeSyncRoot + "losers1");
                    
                    destinationProvider = new FileSyncProvider(aTargetGuid,
                        destinationReplicaRootPath, filter, options,  destinationReplicaRootPath, "files.metadata", CeSyncRoot + "temp2", CeSyncRoot + "losers2");

                    destinationProvider.AppliedChange +=
                        new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
                    destinationProvider.SkippedChange +=
                        new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);

                    Debug.WriteLine(destinationProvider.MetadataFileName);

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

                    Debug.WriteLine("Synchronizing changes to replica: " +
                        destinationProvider.RootDirectoryPath);
                    agent.Synchronize();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("\nException from SyncFileSystemReplicasOneWay:\n" + ex.ToString());
                    Debug.WriteLine(ex.StackTrace);
                }
                finally
                {
                    // Release resources
                    if (sourceProvider != null) sourceProvider.Dispose();
                    if (destinationProvider != null) destinationProvider.Dispose();
                }
            }

       }

    }

    Saturday, March 20, 2010 3:16 PM

Answers

  • here's one and am not getting the behaviour you get:

         public static void Main(string[] args)
        {
            //if (args.Length < 2 ||
            //    string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) ||
            //    !Directory.Exists(args[0]) || !Directory.Exists(args[1]))
            //{
            //    Console.WriteLine(
            //      "Usage: FileSyncSample [valid directory path 1] [valid directory path 2]");
            //    return;
            //}

            //string replica1RootPath = args[0];
            //string replica2RootPath = args[1];
            string replica1RootPath = "C:\\Temp\\Test";
            string replica2RootPath = "C:\\Temp\\Test2";

            try
            {
                // Set options for the sync operation
                FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |
                         FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates ;

                FileSyncScopeFilter filter = new FileSyncScopeFilter();
                filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files

                // Explicitly detect changes on both replicas upfront, to avoid two change
                // detection passes for the two-way sync
                //DetectChangesOnFileSystemReplica(
                //    replica1RootPath, filter, options);
                //DetectChangesOnFileSystemReplica(
                //    replica2RootPath, filter, options);

                //// Sync in both directions
                SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options);
                SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString());
                Console.ReadKey();
            }
        }

        public static void SyncFileSystemReplicasOneWay(
              string sourceReplicaRootPath, string destinationReplicaRootPath,
              FileSyncScopeFilter filter, FileSyncOptions options)
        {
            FileSyncProvider sourceProvider = null;
            FileSyncProvider destinationProvider = null;

            try
            {
                sourceProvider = new FileSyncProvider(
                    //new Guid("f791d60c-2fc9-40d4-afe2-4d601982eea2"),
                    Guid.NewGuid(),
                    sourceReplicaRootPath,
                    filter,
                    options,
                    "C:\\Temp\\myMeta\\",
                    "test.metadata",
                    "C:\\Temp\\Temp\\",
                    "C:\\Temp\\Loser\\"
                    );
                destinationProvider = new FileSyncProvider(
                    //new Guid("f6b2f85f-511f-42df-86c6-494e10bbac48"),
                    Guid.NewGuid(),
                    destinationReplicaRootPath,
                    filter,
                    options,
                    "C:\\Temp\\myMeta\\",
                    "test2.metadata",
                    "C:\\Temp\\Temp2\\",
                    "C:\\Temp\\Loser2\\"
                    );

                sourceProvider.DetectChanges();
                destinationProvider.DetectChanges();


                destinationProvider.AppliedChange +=
                    new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
                destinationProvider.SkippedChange +=
                    new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);

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

                Console.WriteLine("Synchronizing changes to replica: " +
                    destinationProvider.RootDirectoryPath);
                agent.Synchronize();
            }
            finally
            {
                // Release resources
                if (sourceProvider != null) sourceProvider.Dispose();
                if (destinationProvider != null) destinationProvider.Dispose();
            }
        }

     

    Sunday, March 21, 2010 2:17 AM
    Moderator

All replies

  • you specified FileSyncOptions.ExplicitDetectChanges in your options. Can you add a line to explicitly call DetectChanges prior to calling synchronize? (e.g., sourceProvider.DetectChanges)
    Saturday, March 20, 2010 3:28 PM
    Moderator
  • I cant find a GetChanges, but maybe you ment GetChangeBatch? I'll have a look at that one.
    Though I guess the FileSyncOptions.ExplicitDetectChanges is for the bits of code I left out in my example.
    The actual code of the button1_click handler is (see also http://msdn.microsoft.com/en-us/sync/bb887623.aspx)

    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    String replica1RootPath = "c:\test";
                    String replica2RootPath = "c:\temp";
                    // Set options for the synchronization operation
                    FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |
                             FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates ;

                    FileSyncScopeFilter filter = new FileSyncScopeFilter();
                    filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files

                    // Explicitly detect changes on both replicas upfront, to avoid two change
                    // detection passes for the two-way synchronization
                   
                    DetectChangesOnFileSystemReplica(sourceGuid, replica1RootPath, filter, options);
                    DetectChangesOnFileSystemReplica(targetGuid, replica2RootPath, filter, options);
                   
                    // Synchronization in both directions
                    SyncFileSystemReplicasOneWay(sourceGuid, targetGuid,  replica1RootPath, replica2RootPath, null, options);
                    SyncFileSystemReplicasOneWay(targetGuid, sourceGuid, replica2RootPath, replica1RootPath, null, options);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("\nException from File Synchronization Provider:\n" + ex.ToString());
                    Debug.WriteLine(ex.StackTrace);
                }
            }

    Saturday, March 20, 2010 6:48 PM
  • If I add sourceProvider.DetectChanges(); and destinationProvider.DetectChanges(); just after their contructors then it "works".

    Differences are replicated both ways. But again trough the means of filesync.metadate, which is created in the source folder. the files.metadata is created too but then treated as a normal file and synchronized to the destination...

    Does anyone have a simple working example of synchronizing with the metadata file in a different location?

    Thanks!

    Saturday, March 20, 2010 7:21 PM
  • sorry, I meant DetectChanges
    Sunday, March 21, 2010 1:01 AM
    Moderator
  • here's one and am not getting the behaviour you get:

         public static void Main(string[] args)
        {
            //if (args.Length < 2 ||
            //    string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) ||
            //    !Directory.Exists(args[0]) || !Directory.Exists(args[1]))
            //{
            //    Console.WriteLine(
            //      "Usage: FileSyncSample [valid directory path 1] [valid directory path 2]");
            //    return;
            //}

            //string replica1RootPath = args[0];
            //string replica2RootPath = args[1];
            string replica1RootPath = "C:\\Temp\\Test";
            string replica2RootPath = "C:\\Temp\\Test2";

            try
            {
                // Set options for the sync operation
                FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |
                         FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates ;

                FileSyncScopeFilter filter = new FileSyncScopeFilter();
                filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files

                // Explicitly detect changes on both replicas upfront, to avoid two change
                // detection passes for the two-way sync
                //DetectChangesOnFileSystemReplica(
                //    replica1RootPath, filter, options);
                //DetectChangesOnFileSystemReplica(
                //    replica2RootPath, filter, options);

                //// Sync in both directions
                SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options);
                SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString());
                Console.ReadKey();
            }
        }

        public static void SyncFileSystemReplicasOneWay(
              string sourceReplicaRootPath, string destinationReplicaRootPath,
              FileSyncScopeFilter filter, FileSyncOptions options)
        {
            FileSyncProvider sourceProvider = null;
            FileSyncProvider destinationProvider = null;

            try
            {
                sourceProvider = new FileSyncProvider(
                    //new Guid("f791d60c-2fc9-40d4-afe2-4d601982eea2"),
                    Guid.NewGuid(),
                    sourceReplicaRootPath,
                    filter,
                    options,
                    "C:\\Temp\\myMeta\\",
                    "test.metadata",
                    "C:\\Temp\\Temp\\",
                    "C:\\Temp\\Loser\\"
                    );
                destinationProvider = new FileSyncProvider(
                    //new Guid("f6b2f85f-511f-42df-86c6-494e10bbac48"),
                    Guid.NewGuid(),
                    destinationReplicaRootPath,
                    filter,
                    options,
                    "C:\\Temp\\myMeta\\",
                    "test2.metadata",
                    "C:\\Temp\\Temp2\\",
                    "C:\\Temp\\Loser2\\"
                    );

                sourceProvider.DetectChanges();
                destinationProvider.DetectChanges();


                destinationProvider.AppliedChange +=
                    new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
                destinationProvider.SkippedChange +=
                    new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);

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

                Console.WriteLine("Synchronizing changes to replica: " +
                    destinationProvider.RootDirectoryPath);
                agent.Synchronize();
            }
            finally
            {
                // Release resources
                if (sourceProvider != null) sourceProvider.Dispose();
                if (destinationProvider != null) destinationProvider.Dispose();
            }
        }

     

    Sunday, March 21, 2010 2:17 AM
    Moderator
  • Great, thanks!

    I'll try it!

    Which .net framework are you using? I'm using 2.0.

    Monday, March 22, 2010 8:50 AM
  • Not a clue why but your example works just great!

    I'll change your script step-by-step to mine in order to find why yours is working and mine isn't.

    Thanks a lot!!

     

    (removed the following lines from the code of JuneT:

    destinationProvider.AppliedChange +=
                    new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
                destinationProvider.SkippedChange +=
                    new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);

    )
    Monday, March 22, 2010 11:31 AM
  • if you're using this call to detect changes:   DetectChangesOnFileSystemReplica(sourceGuid, replica1RootPath, filter, options);

    the above method doesnt pass the metadata location to the provider prior to calling DetectChanges.

    Tuesday, March 23, 2010 8:23 AM
    Moderator
  • hmm, in my - not working - test, I had already commented out the calls to "DetectChangesOnFileSystemReplica".

    See message of Saturday, March 20, 2010 3:16 PM

    It only calls " SyncFileSystemReplicasOneWay".

    Well, didn't get to finding the difference yet, but will try to do so soon.

    Tuesday, March 23, 2010 8:36 AM