Microsoft同步框架:元数据存储副本已经在使用中

本文关键字:副本 框架 同步 元数据 存储 Microsoft | 更新日期: 2023-09-27 18:12:07

一点平台信息-我使用Visual Studio 2015, Windows 10 Pro和最新版本的MS Sync Framework from Nuget。

using Microsoft.Synchronization;
using Microsoft.Synchronization.Files;
using System.IO;
namespace SyncEngine_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string myDocsPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string Dir1 = Path.Combine(myDocsPath, "Dir1");
            string Dir2 = Path.Combine(myDocsPath, "Dir2");
            string metadataPath = Path.Combine(myDocsPath, "Metadata");
            string tempDir = Path.Combine(myDocsPath, "Cache");
            string trashDir = Path.Combine(myDocsPath, "Trash");
            FileSyncScopeFilter filter = new FileSyncScopeFilter();
            filter.FileNameExcludes.Add("*.metadata");
            FileSyncOptions options = FileSyncOptions.None;
            //DetectChanges
            DetectChangesOnFileSystemReplica(Dir1, filter, options, Dir1, "filesync.metadata", tempDir, trashDir);
            DetectChangesOnFileSystemReplica(Dir2, filter, options, Dir2, "filesync.metadata", tempDir, trashDir);
            //SyncChanges Both Ways
            SyncFileSystemReplicasOneWay(Dir1, Dir2, filter, options, Dir1, "filesync.metadata", tempDir, trashDir);
            SyncFileSystemReplicasOneWay(Dir2, Dir1, filter, options, Dir2, "filesync.metadata", tempDir, trashDir);
        }
        public static void DetectChangesOnFileSystemReplica(string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options, string metadataPath, string metadataFile, string tempDir, string trashDir)
        {
            FileSyncProvider provider = null;
            try
            {
                provider = new FileSyncProvider(replicaRootPath, filter, options, metadataPath, metadataFile, tempDir, trashDir);
                provider.DetectChanges();
            }
            finally
            {
                // Release resources
                if (provider != null)
                    provider.Dispose();
            }
        }
        public static void SyncFileSystemReplicasOneWay(string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options, string metadataPath, string metadataFile, string tempDir, string trashDir)
        {
            FileSyncProvider sourceProvider = null;
            FileSyncProvider destinationProvider = null;
            try
            {
                sourceProvider = new FileSyncProvider(sourceReplicaRootPath, filter, options, metadataPath, metadataFile, tempDir, trashDir);
                destinationProvider = new FileSyncProvider(destinationReplicaRootPath, filter, options, metadataPath, metadataFile, tempDir, trashDir);
                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();
            }
        }
    }

我正试图编写一个自定义文件同步程序,每当我试图偏离微软的默认示例时,我就会遇到问题。默认情况下,在检查更改和同步时,将使用名为filesync的文件。元数据是在源和目标的根中创建的。FileSyncProvider支持指定自定义元数据路径(不能为空),以及自定义缓存/垃圾路径。这就是我真正想要的,但是我必须指定一个特定的元数据位置和文件名。

当我以这种方式运行它时,我得到一个异常,元数据存储副本已经在使用。当我在不同的文件夹中指定不同的名称,相同的名称,甚至默认路径中的默认名称时,就会发生这种情况。下面是我的代码示例:

我以前见过这个错误报告,但从来没有一个好的解决方案,这让我有点担心。我不知道是我做错了什么,还是框架有问题,从来没有解决过,还是Windows 10才是罪魁祸首。

同样,默认的用法工作得很好,但是我不能设置自定义路径:

            // Default Implementation - WHICH WORKS
            // 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, filter, options);
            SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, filter, options);
在这一点上,我愿意接受任何建议,甚至是微软同步的替代品。谢谢你的帮助!

Microsoft同步框架:元数据存储副本已经在使用中

有时候你只需要走开,就会意识到你犯了一个非常愚蠢的疏忽。创建SyncFileSystemReplicasOneWay() void的方式导致我的destinationProvider使用完全相同的元数据文件/目录。我已经这样修改了方法参数:

旧方式-不工作:

public static void SyncFileSystemReplicasOneWay(string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options, string metadataPath, string metadataFile, string tempDir, string trashDir){}

正确的方法-注意的文件名和文件夹路径的添加元数据文件:

public static void SyncFileSystemReplicasOneWay(string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options, string metadataPath1, string metadataFile1, string metadataPath2,string metadataFile2, string tempDir, string trashDir) {}

下面是显示源和目标提供程序对象创建的较大代码片段:

    public static void SyncFileSystemReplicasOneWay(string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options, string metadataPath1, string metadataFile1, string metadataPath2, string metadataFile2, string tempDir, string trashDir)
    {
        sourceProvider = new FileSyncProvider(sourceReplicaRootPath, filter, options, metadataPath1, metadataFile1, tempDir, trashDir);
        destinationProvider = new FileSyncProvider(destinationReplicaRootPath, filter, options, metadataPath2, metadataFile2, tempDir, trashDir);
    }

这是我粗心的疏忽。