ZeroInstall.DesktopIntegration.SyncIntegrationManager.Sync C# (CSharp) Method

Sync() public method

Synchronize the AppList with the sync server and (un)apply AccessPoints accordingly.
The user canceled the task. A problem occurred while deserializing the XML data or the specified crypto key was wrong. A problem occured while communicating with the sync server or while downloading additional data (such as icons). A problem occurs while writing to the filesystem or registry. Write access to the filesystem or registry is not permitted.
public Sync ( SyncResetMode resetMode = SyncResetMode.None ) : void
resetMode SyncResetMode Controls how synchronization data is reset.
return void
        public void Sync(SyncResetMode resetMode = SyncResetMode.None)
        {
            if (!_server.IsValid) throw new InvalidDataException(Resources.PleaseConfigSync);

            var appListUri = new Uri(_server.Uri, new Uri(MachineWide ? "app-list-machine" : "app-list", UriKind.Relative));
            using (var webClient = new WebClientTimeout
            {
                Credentials = _server.Credentials,
                CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
            })
            {
                ExceptionUtils.Retry<WebRaceConditionException>(delegate
                {
                    Handler.CancellationToken.ThrowIfCancellationRequested();

                    byte[] appListData;
                    try
                    {
                        appListData = DownloadAppList(appListUri, webClient, resetMode);
                    }
                        #region Error handling
                    catch (WebException ex)
                        when (ex.Status == WebExceptionStatus.ProtocolError && (ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        Handler.CancellationToken.ThrowIfCancellationRequested();
                        throw new WebException(Resources.SyncCredentialsInvalid, ex, ex.Status, ex.Response);
                    }
                    #endregion

                    HandleDownloadedAppList(resetMode, appListData);

                    try
                    {
                        UploadAppList(appListUri, webClient, resetMode);
                    }
                        #region Error handling
                    catch (WebException ex)
                        when (ex.Status == WebExceptionStatus.ProtocolError && (ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.PreconditionFailed)
                    {
                        Handler.CancellationToken.ThrowIfCancellationRequested();
                        throw new WebRaceConditionException(ex);
                    }
                    #endregion
                }, maxRetries: 3);
            }

            // Save reference point for future syncs
            AppList.SaveXml(AppListPath + AppListLastSyncSuffix);

            Handler.CancellationToken.ThrowIfCancellationRequested();
        }

Usage Example

        /// <summary>
        /// Tests the sync logic with custom <see cref="AppList"/>s.
        /// </summary>
        /// <param name="resetMode">The <see cref="SyncResetMode"/> to pass to <see cref="SyncIntegrationManager.Sync"/>.</param>
        /// <param name="appListLocal">The current local <see cref="AppList"/>.</param>
        /// <param name="appListLast">The state of the <see cref="AppList"/> after the last successful sync.</param>
        /// <param name="appListServer">The current server-side <see cref="AppList"/>.</param>
        private void TestSync(SyncResetMode resetMode, AppList appListLocal, AppList appListLast, AppList appListServer)
        {
            appListLocal.SaveXml(_appListPath);
            if (appListLast != null) appListLast.SaveXml(_appListPath + SyncIntegrationManager.AppListLastSyncSuffix);

            using (var stream = File.Create(_appListPath + ".zip"))
                appListServer.SaveXmlZip(stream);

            using (var appListServerFile = File.OpenRead(_appListPath + ".zip"))
            using (var syncServer = new MicroServer("app-list", appListServerFile))
            {
                using (var integrationManager = new SyncIntegrationManager(_appListPath, new SyncServer {Uri = syncServer.ServerUri, Username = "******", Password = "******"}, interfaceId => new Feed(), new SilentTaskHandler()))
                    integrationManager.Sync(resetMode);

                appListServer = AppList.LoadXmlZip(syncServer.FileContent);
            }

            appListLocal = XmlStorage.LoadXml<AppList>(_appListPath);
            appListLast = XmlStorage.LoadXml<AppList>(_appListPath + SyncIntegrationManager.AppListLastSyncSuffix);
            Assert.AreEqual(appListLocal, appListServer, "Server and local data should be equal after sync");
            Assert.AreEqual(appListLocal, appListLast, "Last sync snapshot and local data should be equal after sync");
        }
All Usage Examples Of ZeroInstall.DesktopIntegration.SyncIntegrationManager::Sync