【问题标题】:How to sync a web server and a local using Microsoft Sync Framework如何使用 Microsoft Sync Framework 同步 Web 服务器和本地服务器
【发布时间】:2014-06-26 13:23:57
【问题描述】:

我正在开发一个用于同步的 Windows 应用程序。我的应用程序将从 Web 服务器同步到本地计算机,反之亦然。我为此使用 Microsoft Sync Framework。 我已经尝试使用框架在我的本地机器中同步 2 个文件夹并且它们有效。但现在我需要在文件夹(比如本地文件夹“App”)和网络服务器之间进行同步。 我有与数据库通信的所有操作(插入/删除/更新等)的 api。

这是我的同步课程的代码。我需要将 path2 更改为 Web 服务器路径。如何使用现有类来做到这一点?

public void FileSyncMain()//string[] args)
    {           

        string replica1RootPath = @"C:\App";
        string replica2RootPath = @"C:\App test";

        try
        {
            // Set options for the synchronization session. In this case, options specify
            // that the application will explicitly call FileSyncProvider.DetectChanges, and
            // that items should be moved to the Recycle Bin instead of being permanently deleted.
            FileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |
                     FileSyncOptions.RecycleDeletedFiles | FileSyncOptions.RecyclePreviousFileOnUpdates |
                     FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.CompareFileStreams;

            // Create a filter that excludes all *.lnk files. The same filter should be used 
            // by both providers.
            FileSyncScopeFilter filter = new FileSyncScopeFilter();
            filter.FileNameIncludes.Add("*.txt");
            filter.FileNameExcludes.Add("*.lnk");

            // Explicitly detect changes on both replicas before syncyhronization occurs.
            // This avoids two change detection passes for the bidirectional synchronization 
            // that we will perform.
            DetectChangesOnFileSystemReplica(
                replica1RootPath, filter, options);
            DetectChangesOnFileSystemReplica(
                replica2RootPath, filter, options);

            // Synchronize the replicas in both directions. In the first session replica 1 is
            // the source, and in the second session replica 2 is the source. The third parameter
            // (the filter value) is null because the filter is specified in DetectChangesOnFileSystemReplica().
            SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options);
            SyncFileSystemReplicasOneWay(replica2RootPath, replica1RootPath, null, options);
        }
        catch (Exception e)
        {
            Console.WriteLine("\nException from File Sync Provider:\n" + e.ToString());
            Console.ReadLine();
        }
    }

    // Create a provider, and detect changes on the replica that the provider
    // represents.
    public static void DetectChangesOnFileSystemReplica(
            string replicaRootPath,
            FileSyncScopeFilter filter, FileSyncOptions options)
    {
        FileSyncProvider provider = null;

        try
        {
            provider = new FileSyncProvider("C:\\App", filter, options);
            provider.DetectChanges();
        }
        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
        {
            // Instantiate source and destination providers, with a null filter (the filter
            // was specified in DetectChangesOnFileSystemReplica()), and options for both.
            sourceProvider = new FileSyncProvider(
                sourceReplicaRootPath, filter, options);
            destinationProvider = new FileSyncProvider(
                destinationReplicaRootPath, filter, options);

            // Register event handlers so that we can write information
            // to the console.
            destinationProvider.AppliedChange +=
                new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
            destinationProvider.SkippedChange +=
                new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);

            // Use SyncCallbacks for conflicting items.
            SyncCallbacks destinationCallbacks = destinationProvider.DestinationCallbacks;
            destinationCallbacks.ItemConflicting += new EventHandler<ItemConflictingEventArgs>(OnItemConflicting);
            destinationCallbacks.ItemConstraint += new EventHandler<ItemConstraintEventArgs>(OnItemConstraint);

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

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

    // Provide information about files that were affected by the synchronization session.
    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;
        }
    }

    // Provide error information for any changes that were skipped.
    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();
    }

    // By default, conflicts are resolved in favor of the last writer. In this example,
    // the change from the source in the first session (replica 1), will always
    // win the conflict.
    public static void OnItemConflicting(object sender, ItemConflictingEventArgs args)
    {
        args.SetResolutionAction(ConflictResolutionAction.SourceWins);
        Console.WriteLine("-- Concurrency conflict detected for item " + args.DestinationChange.ItemId.ToString());
        Console.ReadLine();
    }

    public static void OnItemConstraint(object sender, ItemConstraintEventArgs args)
    {
        args.SetResolutionAction(ConstraintConflictResolutionAction.SourceWins);
        Console.WriteLine("-- Constraint conflict detected for item " + args.DestinationChange.ItemId.ToString());
        Console.ReadLine();
    }

请帮帮我!

提前致谢!

【问题讨论】:

    标签: c# synchronization sync desktop-application microsoft-sync-framework


    【解决方案1】:

    如果您在谈论通过 http 进行同步,则不支持。开箱即用的文件同步提供程序不适用于 ftp 和 http

    【讨论】:

    • 有供应商吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多