Esempio n. 1
0
            public SampleSyncAgent(string localProviderConnString, string remoteProviderConnString)
            {
                //Instantiate the sample provider that allows us to create a provider
                //for both of the peers that are being synchronized.
                SampleSyncProvider sampleSyncProvider = new SampleSyncProvider();

                //Instantiate a DbSyncProvider for the local peer and the remote peer.
                //For example, if this code is running at peer1 and is
                //synchronizing with peer2, peer1 would be the local provider
                //and peer2 the remote provider.
                //<snippetOCSv2_CS_Basic_Peer_DbSyncProvider>
                DbSyncProvider localProvider  = new DbSyncProvider();
                DbSyncProvider remoteProvider = new DbSyncProvider();

                //Create a provider by using the SetupSyncProvider on the sample class.
                sampleSyncProvider.SetupSyncProvider(localProviderConnString, localProvider);
                localProvider.SyncProviderPosition = SyncProviderPosition.Local;

                sampleSyncProvider.SetupSyncProvider(remoteProviderConnString, remoteProvider);
                remoteProvider.SyncProviderPosition = SyncProviderPosition.Remote;
                //</snippetOCSv2_CS_Basic_Peer_DbSyncProvider>

                //Specify the local and remote providers that should be synchronized,
                //and the direction and order of changes. In this case, changes are first
                //uploaded from remote to local and then downloaded in the other direction.
                this.LocalProvider  = localProvider;
                this.RemoteProvider = remoteProvider;
                this.Direction      = SyncDirectionOrder.UploadAndDownload;
            }
        public SampleSyncAgent(string localProviderConnString, string remoteProviderConnString)
        {
            //Instantiate the sample provider that allows us to create a provider
            //for both of the peers that are being synchronized.
            SampleSyncProvider sampleSyncProvider = new SampleSyncProvider();

            //Instantiate a DbSyncProvider for the local peer and the remote peer.
            //For example, if this code is running at peer1 and is
            //synchronizing with peer2, peer1 would be the local provider
            //and peer2 the remote provider.
            DbSyncProvider localProvider  = new DbSyncProvider();
            DbSyncProvider remoteProvider = new DbSyncProvider();

            //Create a provider by using the SetupSyncProvider on the sample class.
            sampleSyncProvider.SetupSyncProvider(localProviderConnString, localProvider);
            localProvider.SyncProviderPosition = SyncProviderPosition.Local;

            sampleSyncProvider.SetupSyncProvider(remoteProviderConnString, remoteProvider);
            remoteProvider.SyncProviderPosition = SyncProviderPosition.Remote;

            //Specify event handlers for the ApplyChangeFailed event for each provider.
            //The handlers are used to resolve conflicting rows and log error information.
            //<snippetOCSv2_CS_Peer_Conflicts_DbApplyChangeFailedEventHandler>
            localProvider.ApplyChangeFailed  += new EventHandler <DbApplyChangeFailedEventArgs>(dbProvider_ApplyChangeFailed);
            remoteProvider.ApplyChangeFailed += new EventHandler <DbApplyChangeFailedEventArgs>(dbProvider_ApplyChangeFailed);
            //</snippetOCSv2_CS_Peer_Conflicts_DbApplyChangeFailedEventHandler>

            //Specify the local and remote providers that should be synchronized,
            //and the direction and order of changes. In this case, changes are first
            //uploaded from remote to local and then downloaded in the other direction.
            this.LocalProvider  = localProvider;
            this.RemoteProvider = remoteProvider;
            this.Direction      = SyncDirectionOrder.UploadAndDownload;

            _localProviderDatabase  = localProvider.Connection.Database.ToString();
            _remoteProviderDatabase = remoteProvider.Connection.Database.ToString();
        }
        static void Main(string[] args)
        {
            //The Utility class handles all functionality that is not
            //directly related to synchronization, such as holding peerConnection
            //string information and making changes to the server database.
            Utility util = new Utility();

            util.RecreateCePeerDatabase(util.Ce1ConnString);

            //<snippetOCSv2_CS_Peer_Cleanup_Synchronize>
            //The SampleStats class handles information from the SyncStatistics
            //object that the Synchronize method returns.
            SampleStats        sampleStats        = new SampleStats();
            SampleSyncProvider sampleSyncProvider = new SampleSyncProvider();

            try
            {
                //Initial synchronization. Instantiate the SyncOrchestrator
                //and call Synchronize.
                sampleSyncProvider = new SampleSyncProvider();
                SyncOrchestrator        sampleSyncAgent;
                SyncOperationStatistics syncStatistics;

                //The integer passed to ConfigureDbSyncProvider is how old that metadata
                //can be (in days) before it is deleted when CleanupMetadata() is called.
                //The integer value is only relevant if CleanupMetadata() is called, as
                //demonstrated later in this application.
                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString, 7),
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer3ConnString, 7));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "initial");

                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer3ConnString, 7),
                    sampleSyncProvider.ConfigureCeSyncProvider(util.Ce1ConnString));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "initial");
            }


            catch (DbOutdatedSyncException ex)
            {
                Console.WriteLine("Outdated Knowledge: " + ex.OutdatedPeerSyncKnowledge.ToString() +
                                  " Clean up knowledge: " + ex.MissingCleanupKnowledge.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            //</snippetOCSv2_CS_Peer_Cleanup_Synchronize>


            //Update a row on peer 1.
            util.MakeDataChangesOnPeer(util.Peer1ConnString, "Customer");


            //Instantiate a provider, connect to peer 1, and delete tombstone metadata that
            //is older than 7 days.
            //<snippetOCSv2_CS_Peer_Cleanup_CleanupMetadata>
            sampleSyncProvider = new SampleSyncProvider();
            DbSyncProvider provider1 = sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString, 7);

            if (provider1.CleanupMetadata() == true)
            {
                Console.WriteLine(String.Empty);
                Console.WriteLine("Metadata cleanup ran in the SyncSamplesDb_Peer1 database.");
                Console.WriteLine("Metadata more than 7 days old was deleted.");
            }
            else
            {
                Console.WriteLine("Metadata cleanup failed, most likely due to concurrency issues.");
            }
            //</snippetOCSv2_CS_Peer_Cleanup_CleanupMetadata>

            //Synchronize.
            try
            {
                SyncOrchestrator        sampleSyncAgent;
                SyncOperationStatistics syncStatistics;

                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString, 7),
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer3ConnString, 7));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "subsequent");
            }


            catch (DbOutdatedSyncException ex)
            {
                Console.WriteLine("Outdated Knowledge: " + ex.OutdatedPeerSyncKnowledge.ToString() +
                                  " Clean up knowledge: " + ex.MissingCleanupKnowledge.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            //Delete a row on peer 3.
            util.MakeDataChangesOnPeer(util.Peer3ConnString, "Customer");


            //Instantiate a provider, connect to peer 3, and delete all tombstone metadata.
            sampleSyncProvider = new SampleSyncProvider();
            DbSyncProvider provider3 = sampleSyncProvider.ConfigureDbSyncProvider(util.Peer3ConnString, -1);


            if (provider3.CleanupMetadata() == true)
            {
                Console.WriteLine(String.Empty);
                Console.WriteLine("Metadata cleanup ran in the SyncSamplesDb_Peer3 database.");
                Console.WriteLine("All metadata was deleted.");
            }
            else
            {
                Console.WriteLine("Metadata cleanup failed, most likely due to concurrency issues.");
            }


            //Synchronize.
            try
            {
                SyncOrchestrator        sampleSyncAgent;
                SyncOperationStatistics syncStatistics;

                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString, 7),
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer3ConnString, 7));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "subsequent");
            }


            catch (DbOutdatedSyncException ex)
            {
                Console.WriteLine(String.Empty);
                Console.WriteLine("Synchronization failed due to outdated synchronization knowledge,");
                Console.WriteLine("which is expected in this sample application.");
                Console.WriteLine("Drop and recreate the sample databases.");
                Console.WriteLine(String.Empty);
                Console.WriteLine("Outdated Knowledge: " + ex.OutdatedPeerSyncKnowledge.ToString() +
                                  " Clean up knowledge: " + ex.MissingCleanupKnowledge.ToString());
                Console.WriteLine(String.Empty);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //Return peer data back to its original state.
            util.CleanUpPeer(util.Peer1ConnString);
            util.CleanUpPeer(util.Peer3ConnString);

            //Exit.
            Console.Write("\nPress Enter to close the window.");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            //The Utility class handles all functionality that is not
            //directly related to synchronization, such as holding peerConnection
            //string information and making changes to the server database.
            Utility util = new Utility();

            util.RecreateCePeerDatabase(util.Ce1ConnString);
            util.RecreateCePeerDatabase(util.Ce2ConnString);

            //<snippetOCSv2_CS_Basic_PeerWithCe_Synchronize>
            //The SampleStats class handles information from the SyncStatistics
            //object that the Synchronize method returns.
            SampleStats sampleStats = new SampleStats();

            try
            {
                //Initial synchronization. Instantiate the SyncOrchestrator
                //and call Synchronize.
                SampleSyncProvider      sampleSyncProvider = new SampleSyncProvider();
                SyncOrchestrator        sampleSyncAgent;
                SyncOperationStatistics syncStatistics;

                //First session: Synchronize two databases by using two instances
                //of DbSyncProvider.
                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString),
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer2ConnString));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "initial");

                //<snippetOCSv2_CS_Basic_PeerWithCe_SnapshotInit>
                //Second session: Synchronize two databases by using one instance of
                //DbSyncProvider and one instance of SqlCeSyncProvider. After the Compact
                //database is initialized, it is copied by using GenerateSnapshot and then
                //used for the third session.
                SqlCeSyncProvider sqlCeSyncProvider1 = sampleSyncProvider.ConfigureCeSyncProvider(util.Ce1ConnString);

                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString),
                    sqlCeSyncProvider1);
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "initial");

                //Copy the Compact database and save it as SyncSampleCe2.sdf.
                SqlCeSyncStoreSnapshotInitialization snapshotInit = new SqlCeSyncStoreSnapshotInitialization();
                snapshotInit.GenerateSnapshot(new SqlCeConnection(util.Ce1ConnString), "SyncSampleCe2.sdf");

                //Make a change that is synchronized during the third session.
                util.MakeDataChangesOnPeer(util.Peer1ConnString, "Customer");

                //Third session: Synchronize the new Compact database. The five rows
                //from SyncSampleCe1.sdf are already in SyncSampleCe2.sdf. The new
                //change is now downloaded to bring SyncSampleCe2.sdf up to date.
                //SyncSampleCe2.sdf will get this row during the next round of
                //synchronization sessions.
                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString),
                    sampleSyncProvider.ConfigureCeSyncProvider(util.Ce2ConnString));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "initial");
                //</snippetOCSv2_CS_Basic_PeerWithCe_SnapshotInit>
            }


            catch (DbOutdatedSyncException ex)
            {
                Console.WriteLine("Outdated Knowledge: " + ex.OutdatedPeerSyncKnowledge.ToString() +
                                  " Clean up knowledge: " + ex.MissingCleanupKnowledge.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            //</snippetOCSv2_CS_Basic_PeerWithCe_Synchronize>

            //Make a change in one of the databases.
            util.MakeDataChangesOnPeer(util.Peer2ConnString, "Customer");

            try
            {
                //Subsequent synchronization. Changes are now synchronized between all
                //nodes.
                SampleSyncProvider      sampleSyncProvider = new SampleSyncProvider();
                SyncOrchestrator        sampleSyncAgent;
                SyncOperationStatistics syncStatistics;

                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString),
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer2ConnString));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "subsequent");

                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString),
                    sampleSyncProvider.ConfigureCeSyncProvider(util.Ce1ConnString));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "subsequent");

                sampleSyncAgent = new SampleSyncAgent(
                    sampleSyncProvider.ConfigureDbSyncProvider(util.Peer1ConnString),
                    sampleSyncProvider.ConfigureCeSyncProvider(util.Ce2ConnString));
                syncStatistics = sampleSyncAgent.Synchronize();
                sampleStats.DisplayStats(syncStatistics, "subsequent");
            }


            catch (DbOutdatedSyncException ex)
            {
                Console.WriteLine("Outdated Knowledge: " + ex.OutdatedPeerSyncKnowledge.ToString() +
                                  " Clean up knowledge: " + ex.MissingCleanupKnowledge.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            //Return data back to its original state.
            util.CleanUpPeer(util.Peer1ConnString);
            util.CleanUpPeer(util.Peer2ConnString);

            //Exit.
            Console.Write("\nPress Enter to close the window.");
            Console.ReadLine();
        }