다음을 통해 공유


File Synchronization Provider와 다른 공급자의 동기화

이 항목에서는 File Synchronization Provider가 다른 Sync Framework 공급자와 동기화되도록 하는 관리되는 응용 프로그램을 만드는 방법을 설명합니다. 이 경우에는 다른 공급자가 단순 공급자이지만 표준 사용자 지정 공급자를 사용할 수도 있습니다. 단순 공급자에 대한 자세한 내용은 단순 사용자 지정 공급자 구현을 참조하십시오.

응용 프로그램에 대한 두 가지 주요 요구 사항이 있습니다.

  • 응용 프로그램에서 두 공급자 간의 데이터 전송 인터페이스로 IFileDataRetriever를 구현해야 합니다.

  • 단순 공급자는 File Synchronization Provider가 사용하는 것과 동일한 형식의 ID를 사용해야 합니다. 복제본 ID의 경우 16바이트 GUID, 항목 ID의 경우 8바이트 접두사가 추가된 16바이트 GUID, 변경 단위 ID의 경우 4바이트 정수입니다.

관리 코드 예제

이 항목의 예제에서는 File Synchronization Provider와 단순 공급자의 동기화와 관련하여 ID와 전송 인터페이스 요구 사항을 중점적으로 일부 코드를 설명합니다. 전체 응용 프로그램의 맥락에서 이 코드를 보려면 Code Gallery에서 사용 가능한 "Sync 101 - Synchronizing a File Synchronization Provider with a Simple Provider" 응용 프로그램을 참조하십시오.

다음 코드 예제에서는 단순 공급자의 IdFormats 속성 정의가 앞에서 설명한 요구 사항을 충족하는 방법을 보여 줍니다.

public override SyncIdFormatGroup IdFormats
{
    get
    {
        SyncIdFormatGroup idFormats = new SyncIdFormatGroup();
        idFormats.ItemIdFormat.Length = 24;
        idFormats.ItemIdFormat.IsVariableLength = false;
        idFormats.ReplicaIdFormat.Length = 16;
        idFormats.ReplicaIdFormat.IsVariableLength = false;
        idFormats.ChangeUnitIdFormat.Length = 4;
        idFormats.ChangeUnitIdFormat.IsVariableLength = false;

        return idFormats;
    }
}
Public Overrides ReadOnly Property IdFormats() As SyncIdFormatGroup
    Get
        Dim FormatGroup As New SyncIdFormatGroup()

        FormatGroup.ItemIdFormat.Length = 24
        FormatGroup.ItemIdFormat.IsVariableLength = False
        FormatGroup.ReplicaIdFormat.Length = 16
        FormatGroup.ReplicaIdFormat.IsVariableLength = False
        FormatGroup.ChangeUnitIdFormat.Length = 4
        FormatGroup.ChangeUnitIdFormat.IsVariableLength = False

        Return FormatGroup
    End Get
End Property

다음 코드 예제에서는 LoadChangeDataInsertItem의 단순 공급자에 필요한 메서드 두 개를 보여 줍니다. 이 메서드의 코드 대부분은 단순 공급자를 구현하기 위한 것이지만, 주목해야 할 두 가지 부분이 있습니다.

  • LoadChangeData는 로컬 저장소에서 열거된 데이터를 로드하는 데 사용됩니다(해당 저장소는 단순 공급자가 서비스함). 이 메서드는 데이터를 샘플의 IFileDataRetriever 구현인 SimpleFileDataRetriever 개체로 반환합니다.

    public override object LoadChangeData(ItemFieldDictionary keyAndExpectedVersion,
        IEnumerable<SyncId> changeUnitsToLoad,
        RecoverableErrorReportingContext recoverableErrorReportingContext)
    {
        // Figure out which item is being asked for
    
        string localRelativePath;
        long expectedLMT;
        ParseDictionary(keyAndExpectedVersion, out localRelativePath, out expectedLMT);
    
        string localPath = Path.Combine(this.rootFolder, localRelativePath);
        string currentVersion = File.GetLastWriteTimeUtc(localPath).Ticks.ToString();
    
        // Check if it changed --- race condition!
    
        if (File.GetLastWriteTimeUtc(localPath).Ticks != expectedLMT)
        {
            recoverableErrorReportingContext.RecordRecoverableErrorForChange(
                new RecoverableErrorData(null));
            return null;
        }
    
        // Return
        return new SimpleFileDataRetriever(localRelativePath, null, localPath, File.GetAttributes(localPath));
    }
    
    Public Overrides Function LoadChangeData(ByVal keyAndExpectedVersion As ItemFieldDictionary, ByVal changeUnitsToLoad As IEnumerable(Of SyncId), ByVal recoverableErrorReportingContext As RecoverableErrorReportingContext) As Object
        ' Figure out which item is being asked for 
    
        Dim localRelativePath As String = ""
        Dim expectedLMT As Long
        ParseDictionary(keyAndExpectedVersion, localRelativePath, expectedLMT)
    
        Dim localPath As String = Path.Combine(Me.rootFolder, localRelativePath)
        Dim currentVersion As String = File.GetLastWriteTimeUtc(localPath).Ticks.ToString()
    
        ' Check if it changed --- race condition! 
    
        If File.GetLastWriteTimeUtc(localPath).Ticks <> expectedLMT Then
            recoverableErrorReportingContext.RecordRecoverableErrorForChange(Nothing)
            Return Nothing
        End If
    
        ' Return 
        Return New SimpleFileDataRetriever(localRelativePath, Nothing, localPath, File.GetAttributes(localPath))
    End Function
    
  • InsertItem은 원격 저장소에서 로컬 저장소로 데이터를 삽입하는 데 사용됩니다(저장소는 File Synchronization Provider가 서비스함). 이 메서드는 받은 항목 데이터를 IFileDataRetriever 개체로 캐스팅합니다. InsertItem 메서드도 데이터를 캐스팅합니다.

    public override void InsertItem(object itemData,
        IEnumerable<SyncId> changeUnitsToCreate,
        RecoverableErrorReportingContext recoverableErrorReportingContext,
        out ItemFieldDictionary keyAndUpdatedVersion,
        out bool commitKnowledgeAfterThisItem)
    {
        // Figure out where to create it
    
        IFileDataRetriever fileData = (IFileDataRetriever)itemData;
    
        string localPath = Path.Combine(this.rootFolder, Path.Combine(fileData.RelativeDirectoryPath, fileData.FileData.Name));
    
        // Check if it is already there --- name collision
    
        if (File.Exists(localPath))
        {
            recoverableErrorReportingContext.RecordConstraintError(
                ConstructDictionary(Path.Combine(fileData.RelativeDirectoryPath, fileData.FileData.Name)));
    
            keyAndUpdatedVersion = null;
            commitKnowledgeAfterThisItem = false;
    
            return;
        }
    
        // Create it
    
        File.Copy(fileData.AbsoluteSourceFilePath, localPath);
    
        // Return particulars to Simple Provider framework
    
        keyAndUpdatedVersion = ConstructDictionary(
            Path.Combine(fileData.RelativeDirectoryPath, fileData.FileData.Name),
            File.GetLastWriteTimeUtc(localPath).Ticks);
    
        commitKnowledgeAfterThisItem = false;
    }
    
    Public Overrides Sub InsertItem(ByVal itemData As Object, ByVal changeUnitsToCreate As IEnumerable(Of SyncId), ByVal recoverableErrorReportingContext As RecoverableErrorReportingContext, ByRef keyAndUpdatedVersion As ItemFieldDictionary, ByRef commitKnowledgeAfterThisItem As Boolean)
        ' Figure out where to create it 
    
        Dim fileData As IFileDataRetriever = DirectCast(itemData, IFileDataRetriever)
    
        Dim localPath As String = Path.Combine(Me.rootFolder, Path.Combine(fileData.RelativeDirectoryPath, fileData.FileData.Name))
    
        ' Check if it is already there --- name collision 
    
        If File.Exists(localPath) Then
            recoverableErrorReportingContext.RecordConstraintError(ConstructDictionary(Path.Combine(fileData.RelativeDirectoryPath, fileData.FileData.Name)))
    
            keyAndUpdatedVersion = Nothing
            commitKnowledgeAfterThisItem = False
    
            Exit Sub
        End If
    
        ' Create it 
    
        File.Copy(fileData.AbsoluteSourceFilePath, localPath)
    
        ' Return particulars to Simple Provider framework 
    
        keyAndUpdatedVersion = ConstructDictionary(Path.Combine(fileData.RelativeDirectoryPath, fileData.FileData.Name), File.GetLastWriteTimeUtc(localPath).Ticks)
    
        commitKnowledgeAfterThisItem = False
    End Sub
    

다음 코드 예제는 AbsoluteSourceFilePathRelativeDirectoryPath를 사용하여 파일 위치를 식별하고 FileDataFileStream을 사용하여 실제 데이터를 전송하는 SimpleFileDataRetriever 클래스를 만듭니다.

class SimpleFileDataRetriever : IFileDataRetriever, IDisposable
{
    private string _relativeLocalFilePath;
    private Stream _sourceStream;
    private string _absoluteSourceFilePath;
    private FileAttributes _attributes;

    public SimpleFileDataRetriever(string relativeLocalFilePath, Stream sourceStream, string absoluteSourceFilePath, FileAttributes attributes)
    { 
        this._relativeLocalFilePath = relativeLocalFilePath;
        this._sourceStream = sourceStream;
        this._attributes = attributes;
        this._absoluteSourceFilePath = absoluteSourceFilePath;
    }

    #region IFileDataRetriever Members

    // If the local store has no concept of absolute file path then return a NotImplementedException here.  
    // The FSP will instead use the stream for file copying.
    // If implemented, return absolute local path including file name.
    public string AbsoluteSourceFilePath
    {
        get 
        { 
            return this._absoluteSourceFilePath; 
        }
    }

    public FileData FileData
    {
        get
        {
            FileInfo fi = new FileInfo(_absoluteSourceFilePath);

            //For the relative path on FileData, provide relative path including file name
            return new FileData(
                _relativeLocalFilePath,
                _attributes,
                fi.CreationTimeUtc,
                fi.LastAccessTimeUtc,
                fi.LastWriteTimeUtc,
                fi.Length);
        }
    }

    public System.IO.Stream FileStream
    {
        get 
        {
            if (this._sourceStream == null)
            {
                this._sourceStream = new FileStream(this._absoluteSourceFilePath, FileMode.Open);
            }

            return _sourceStream; 
        }
    }

    // Must return the relative path without the filename
    public string RelativeDirectoryPath
    {
        get
        {
            return Path.GetDirectoryName(_relativeLocalFilePath);
        }
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    {
        if (this._sourceStream != null)
        {
            this._sourceStream.Close();
        }
    }

    #endregion
}
    Class SimpleFileDataRetriever
        Implements IFileDataRetriever
        '        Implements IDisposable
        Private _relativeLocalFilePath As String
        Private _sourceStream As Stream
        Private _absoluteSourceFilePath As String
        Private _attributes As FileAttributes

        Public Sub New(ByVal relativeLocalFilePath As String, ByVal sourceStream As Stream, ByVal absoluteSourceFilePath As String, ByVal attributes As FileAttributes)
            Me._relativeLocalFilePath = relativeLocalFilePath
            Me._sourceStream = sourceStream
            Me._attributes = attributes
            Me._absoluteSourceFilePath = absoluteSourceFilePath
        End Sub

#Region "IFileDataRetriever Members"

        ' If the local store has no concept of absolute file path then return a NotImplementedException here. 
        ' The FSP will instead use the stream for file copying. 
        ' If implemented, return absolute local path including file name. 
        Public ReadOnly Property AbsoluteSourceFilePath() As String Implements IFileDataRetriever.AbsoluteSourceFilePath
            Get
                Return Me._absoluteSourceFilePath
            End Get
        End Property

        Public ReadOnly Property FileData() As FileData Implements IFileDataRetriever.FileData
            Get
                Dim fi As New FileInfo(_absoluteSourceFilePath)

                'For the relative path on FileData, provide relative path including file name 
                Return New FileData(_relativeLocalFilePath, _attributes, fi.CreationTimeUtc, fi.LastAccessTimeUtc, fi.LastWriteTimeUtc, fi.Length)
            End Get
        End Property

        Public ReadOnly Property FileStream() As System.IO.Stream Implements IFileDataRetriever.FileStream
            Get
                If Me._sourceStream Is Nothing Then
                    Me._sourceStream = New FileStream(Me._absoluteSourceFilePath, FileMode.Open)
                End If

                Return _sourceStream
            End Get
        End Property

        ' Must return the relative path without the filename 
        Public ReadOnly Property RelativeDirectoryPath() As String Implements IFileDataRetriever.RelativeDirectoryPath
            Get
                Return Path.GetDirectoryName(_relativeLocalFilePath)
            End Get
        End Property

#End Region

#Region "IDisposable Members"

        Public Sub Dispose()
            If Me._sourceStream IsNot Nothing Then
                Me._sourceStream.Close()
            End If
        End Sub

#End Region
    End Class

다음 코드 예제에서는 두 공급자를 동기화합니다. 동기화 프로세스는 두 File Synchronization Provider 또는 두 단순 공급자를 동기화하는 경우와 마찬가지입니다. IFileDataRetriever 인터페이스를 구현하고 적절한 ID 형식을 사용하면 데이터가 정확한 방식으로 전송됩니다.

static void DoBidirectionalSync(string pathA, Guid replicaA, string pathB, Guid replicaB)
{
    SyncOperationStatistics stats;
    MySimpleFileProvider providerA = new MySimpleFileProvider(replicaA, pathA);
    FileSyncProvider providerB = new FileSyncProvider(replicaB, pathB);

    //Set the custom provider's conflict resolution policy to custom in order to show 
    //how to perform complex resolution actions.
    providerA.Configuration.ConflictResolutionPolicy = ConflictResolutionPolicy.ApplicationDefined;

    //Register callbacks so that we can handle conflicts if they are detected, and other events.
    RegisterCallbacks(providerA);
    RegisterCallbacks(providerB);

    //Synchronize the two providers that are specified.           
    Console.WriteLine("Sync {0} and {1}...", pathA, pathB);
    SyncOrchestrator agent = new SyncOrchestrator();

    //To avoid writing conflict resolution logic in your matching provider it is good practice to always sync from custom provider
    //to FSP provider first.  That way the FSP will handle all the conflicts itself.  Here we do the opposite to show our custom
    //constraint conflict resolution.
    agent.Direction = SyncDirectionOrder.UploadAndDownload;

    agent.LocalProvider = providerB;
    agent.RemoteProvider = providerA;
    stats = agent.Synchronize();

    //Display the statistics from the SyncOperationStatistics object that is returned 
    //by Synchronize().
    Console.WriteLine("Download Applied:\t {0}", stats.DownloadChangesApplied);
    Console.WriteLine("Download Failed:\t {0}", stats.DownloadChangesFailed);
    Console.WriteLine("Download Total:\t\t {0}", stats.DownloadChangesTotal);
    Console.WriteLine("Upload Total:\t\t {0}", stats.UploadChangesApplied);
    Console.WriteLine("Upload Total:\t\t {0}", stats.UploadChangesFailed);
    Console.WriteLine("Upload Total:\t\t {0}", stats.UploadChangesTotal);
}
Private Shared Sub DoBidirectionalSync(ByVal pathA As String, ByVal replicaA As Guid, ByVal pathB As String, ByVal replicaB As Guid)
    Dim stats As SyncOperationStatistics
    Dim providerA As New MySimpleFileProvider(replicaA, pathA)
    Dim providerB As New FileSyncProvider(replicaB, pathB)

    'Set the custom provider's conflict resolution policy to custom in order to show 
    'how to perform complex resolution actions. 
    providerA.Configuration.ConflictResolutionPolicy = ConflictResolutionPolicy.ApplicationDefined

    'Register callbacks so that we can handle conflicts if they are detected, and other events. 
    RegisterCallbacks(providerA)
    RegisterCallbacks(providerB)

    'Synchronize the two providers that are specified. 
    Console.WriteLine("Sync {0} and {1}...", pathA, pathB)
    Dim agent As New SyncOrchestrator()

    'To avoid writing conflict resolution logic in your matching provider it is good practice to always sync from custom provider 
    'to FSP provider first. That way the FSP will handle all the conflicts itself. Here we do the opposite to show our custom 
    'constraint conflict resolution. 
    agent.Direction = SyncDirectionOrder.UploadAndDownload

    agent.LocalProvider = providerB
    agent.RemoteProvider = providerA
    stats = agent.Synchronize()

    'Display the statistics from the SyncOperationStatistics object that is returned 
    'by Synchronize(). 
    Console.WriteLine("Download Applied:" & vbTab & " {0}", stats.DownloadChangesApplied)
    Console.WriteLine("Download Failed:" & vbTab & " {0}", stats.DownloadChangesFailed)
    Console.WriteLine("Download Total:" & vbTab & vbTab & " {0}", stats.DownloadChangesTotal)
    Console.WriteLine("Upload Total:" & vbTab & vbTab & " {0}", stats.UploadChangesApplied)
    Console.WriteLine("Upload Total:" & vbTab & vbTab & " {0}", stats.UploadChangesFailed)
    Console.WriteLine("Upload Total:" & vbTab & vbTab & " {0}", stats.UploadChangesTotal)
End Sub

참고 항목

개념

단순 사용자 지정 공급자 구현
다른 공급자의 데이터 통합
파일 동기화