none
Metadata already in use error in microsoft sync frame work RRS feed

All replies

  • which providers are you using? what are you trying to sync?
    Tuesday, September 20, 2011 2:58 AM
    Moderator
  • trying to syncronize files between two folder using microsoft sync frame work

    below code is i am using.  using timer to syncronise files at particular interval of time.

     

    class

    FileSyncProviderSample

    {

     

    // static SyncId sourceID;
     

     

    //static SyncId destID;
     

     

    public static void Main(string[] args)

    {

     

    string[] arg = new string[2];

    arg[0] =

    @"C:\Projects\TestFolder\F1";

    arg[1] =

    @"C:\Projects\TestFolder\F2";

     

    // arg[1] = @"\\199.63.39.76\Scanner Test Cases\F2";
     

     

    if (arg.Length < 2 ||

     

    string.IsNullOrEmpty(arg[0]) || string.IsNullOrEmpty(arg[1]) ||

    !

    Directory.Exists(arg[0]) || !Directory.Exists(arg[1]))

    {

     

    Console.WriteLine(

     

    "Usage: FileSyncSample [valid directory path 1] [valid directory path 2]");

     

    Console.ReadLine();

     

    return;

    }

     

    string replica1RootPath = arg[0];

     

    string replica2RootPath = arg[1];

    System.Timers.

    Timer objTimer;

     

    try
     

    {

     

    // Set options for the synchronization operation
     

     

    //WatchDirectory(replica1RootPath, replica2RootPath);
     

     

    objTimer =

    new System.Timers.Timer(1000);

     

    objTimer.Elapsed +=

    new System.Timers.ElapsedEventHandler(objTimer_Elapsed);

    objTimer.Interval = 10000;

    objTimer.Enabled =

    true;

     

    //WatchDirectory(@"C:\Projects\TestFolder\F1", @"C:\Projects\TestFolder\F2");
     

     

    Console.WriteLine("Press the Enter key to exit the program.");

     

    Console.ReadLine();

     

    }

     

    catch (Exception e)

    {

     

    Console.WriteLine("\nException from File Synchronization Provider:\n" + e.ToString());

     

    Console.ReadLine();

    }

    }

     

    private static void WatchDirectory(string replica1RootPath, string replica2RootPath)

    {

     

    FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |

     

    FileSyncOptions.RecycleDeletedFiles |
     

    FileSyncOptions

    .RecyclePreviousFileOnUpdates |

    FileSyncOptions

    .RecycleConflictLoserFiles;

     

    FileSyncScopeFilter filter = new FileSyncScopeFilter();

    filter.FileNameExcludes.Add(

    "*.lnk"); // Exclude all *.lnk files
     

    filter.FileNameExcludes.Add(

    "filesync.metadata");

     

    // Explicitly detect changes on both replicas upfront, to avoid two change
     

     

    // detection passes for the two-way synchronization

     

     

    DetectChangesOnFileSystemReplica(

    replica1RootPath, filter, options);

    DetectChangesOnFileSystemReplica(

    replica2RootPath, filter, options);

     

    // Synchronization in both directions
     

    SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath,

    null, options);

    SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath,

    null, options);

    }

     

    static void objTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

    {

    WatchDirectory(

    @"C:\Projects\TestFolder\F1", @"C:\Projects\TestFolder\F2");

     

     

    }

     

    public static void DetectChangesOnFileSystemReplica(

     

    string replicaRootPath,

     

    FileSyncScopeFilter filter, FileSyncOptions options)

    {

     

    FileSyncProvider provider = null;

     

    try
     

    {

    provider =

    new FileSyncProvider( new System.Guid(),replicaRootPath, filter, options);

    provider.DetectChanges();

    }

     

    catch (Exception ex)

    {

     

    Console.WriteLine(ex.Message);

    }

     

    finally
     

    {

     

    // Release resources
     

     

    if (provider != null)

    provider.Dispose();

    }

    }

     

    public static void SyncFileSystemReplicasOneWay(

     

    string sourceReplicaRootPath, string destinationReplicaRootPath,

     

    FileSyncScopeFilter filter, FileSyncOptions options)

    {

     

    FileSyncProvider sourceProvider = null;

     

    FileSyncProvider destinationProvider = null;

     

    try
     

    {

     

     

     

     

     

    sourceProvider = new FileSyncProvider(sourceReplicaRootPath, filter, options);

    destinationProvider =

    new FileSyncProvider(

    destinationReplicaRootPath, filter, options);

    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;// Upload; // Sync source to destination
     

     

    Console.WriteLine("Synchronizing changes to replica: " +

    destinationProvider.RootDirectoryPath);

     

    //Console.ReadLine();
     

    agent.Synchronize();

    }

     

    catch (Exception ex)

    {

    }

     

    finally

    {

     

    // Release resources

     

    if (sourceProvider != null) { sourceProvider.Dispose(); sourceProvider = null; }

     

    if (destinationProvider != null) { destinationProvider.Dispose(); destinationProvider = null; }

    }

    }

     

    public static void OnAppliedChange(object sender, AppliedChangeEventArgs args)

    {

     

    switch (args.ChangeType)

    {

     

    case ChangeType.Create:

     

    Console.WriteLine("-- Applied CREATE for file " + args.NewFilePath);

     

    break;

     

    case ChangeType.Delete:

     

    Console.WriteLine("-- Applied DELETE for file " + args.OldFilePath);

     

    break;

     

    case ChangeType.Update:

     

    Console.WriteLine("-- Applied OVERWRITE for file " + args.OldFilePath);

     

    break;

     

    case ChangeType.Rename:

     

    Console.WriteLine("-- Applied RENAME for file " + args.OldFilePath +

     

    " as " + args.NewFilePath);

     

    break;

    }

     

    Console.ReadLine();

    }

     

    public static void OnSkippedChange(object sender, SkippedChangeEventArgs args)

    {

     

    Console.WriteLine("-- Skipped applying " + args.ChangeType.ToString().ToUpper()

    +

    " for " + (!string.IsNullOrEmpty(args.CurrentFilePath) ?

    args.CurrentFilePath : args.NewFilePath) +

    " due to error");

     

    if (args.Exception != null)

     

    Console.WriteLine(" [" + args.Exception.Message + "]");

     

    Console.ReadLine();

    }

     

     

     

     

    }

    }

    Wednesday, September 28, 2011 5:15 AM
  • check that you're not having two concurrent syncs on going. specifically, if your timer fires, check that it doesnt fire anew while the sync from the first timer event is not yet complete.
    Wednesday, September 28, 2011 8:15 AM
    Moderator
  • i am checking with only one or two files  .this error is occur only if any file is created on the folder. if there is no change in folders , no error.

     

    Wednesday, September 28, 2011 12:49 PM
  • do a quick check without a timer or disabling the timer while a sync is ongoing. that should give you an idea.
    Wednesday, September 28, 2011 1:09 PM
    Moderator
  • forgive me if i am saying stupid things. Without timer it is  working fine. but  each time need to run the application  for sync the folders.

    My idea is this logic is implemented as windows  service and  sync the folders at particular interval of time. that is why i have used timer.

    Thursday, September 29, 2011 6:44 AM
  • the error you are getting is a concurrency issue. two processes trying to access the metadata at the same time. as i have mentioned, if your timer fires, disable it from firing the second time around while you're synching. then enable the timer again after you sync.
    Thursday, September 29, 2011 7:30 AM
    Moderator
  • Thanks june. it is working fine.
    Friday, September 30, 2011 7:10 AM