/// <summary> /// Method that synchronize the Cache by uploading all modified changes and then downloading the /// server changes. /// </summary> internal async Task <CacheRefreshStatistics> SynchronizeAsync(CancellationToken cancellationToken) { CacheRefreshStatistics statistics = new CacheRefreshStatistics(); try { // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // set start time statistics.StartTime = DateTime.Now; // First create the CacheRequestHandler this.cacheRequestHandler = new HttpCacheRequestHandler(this.serviceUri, this.controllerBehavior); // Then fire the BeginSession call on the local provider. this.localProvider.BeginSession(); // Set the flag to indicate BeginSession was successful this.beginSessionComplete = true; // Do uploads first statistics = await this.EnqueueUploadRequest(statistics, cancellationToken); // Set end time statistics.EndTime = DateTime.Now; // Call EndSession only if BeginSession was successful. if (this.beginSessionComplete) { this.localProvider.EndSession(); } } catch (OperationCanceledException ex) { statistics.EndTime = DateTime.Now; statistics.Cancelled = true; statistics.Error = ex; this.localProvider.EndSession(); } catch (Exception ex) { statistics.EndTime = DateTime.Now; statistics.Error = ex; this.localProvider.EndSession(); } finally { // Reset the state this.ResetAsyncWorkerManager(); } return(statistics); }
/// <summary> /// Reset the state of the objects /// </summary> private void ResetAsyncWorkerManager() { lock (this.lockObject) { this.cacheRequestHandler = null; this.controllerBehavior.Locked = false; this.beginSessionComplete = false; } }
/// <summary> /// Method that synchronize the Cache by uploading all modified changes and then downloading the /// server changes. /// </summary> internal async Task <CacheRefreshStatistics> SynchronizeAsync(CancellationToken cancellationToken, IProgress <SyncProgressEvent> progress = null) { CacheRefreshStatistics statistics = new CacheRefreshStatistics(); // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // set start time statistics.StartTime = DateTime.Now; // Reporting progress if (progress != null) { progress.Report(new SyncProgressEvent(SyncStage.StartingSync, TimeSpan.Zero)); } try { // First create the CacheRequestHandler this.cacheRequestHandler = new HttpCacheRequestHandler(this.serviceUri, this.controllerBehavior); // Then fire the BeginSession call on the local provider. await this.localProvider.BeginSession(); // Set the flag to indicate BeginSession was successful this.beginSessionComplete = true; // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // Do uploads first (no batch mode on Upload Request) statistics = await this.EnqueueUploadRequest(statistics, cancellationToken, progress); // If there is an error during Upload request, dont want to donwload if (statistics.Error != null) { throw new Exception("Error occured during Upload request.", statistics.Error); } // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // When there are records records that are not uploading successfully // a successfull download 1-N records will cause anchor to be updated. // if anchor is updated then the records on the device will not be attempted for upload again // on subsequent syncs. therefore check for null Error if (statistics.Error == null) { // then Download (be careful, could be in batch mode) statistics = await this.EnqueueDownloadRequest(statistics, cancellationToken, progress); } // Set end time statistics.EndTime = DateTime.Now; // Call EndSession only if BeginSession was successful. if (this.beginSessionComplete) { this.localProvider.EndSession(); } } catch (OperationCanceledException ex) { statistics.EndTime = DateTime.Now; statistics.Cancelled = true; statistics.Error = ex; if (this.beginSessionComplete) { this.localProvider.EndSession(); } } catch (Exception ex) { statistics.EndTime = DateTime.Now; statistics.Error = ex; if (this.beginSessionComplete) { this.localProvider.EndSession(); } } finally { // Reset the state this.ResetAsyncWorkerManager(); } // Reporting progress if (progress != null) { progress.Report(new SyncProgressEvent(SyncStage.EndingSync, statistics.EndTime.Subtract(statistics.StartTime))); } return(statistics); }
/// <summary> /// Method that synchronize the Cache by uploading all modified changes and then downloading the /// server changes. /// </summary> internal async Task<CacheRefreshStatistics> SynchronizeAsync(CancellationToken cancellationToken, IProgress<SyncProgressEvent> progress = null) { CacheRefreshStatistics statistics = new CacheRefreshStatistics(); // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested(); // set start time statistics.StartTime = DateTime.Now; // Reporting progress if (progress != null) progress.Report(new SyncProgressEvent(SyncStage.StartingSync, TimeSpan.Zero)); try { // First create the CacheRequestHandler this.cacheRequestHandler = new HttpCacheRequestHandler(this.serviceUri, this.controllerBehavior); // Then fire the BeginSession call on the local provider. await this.localProvider.BeginSession(); // Set the flag to indicate BeginSession was successful this.beginSessionComplete = true; // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested(); // Do uploads first (no batch mode on Upload Request) statistics = await this.EnqueueUploadRequest(statistics, cancellationToken, progress); // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested(); // then Download (be careful, could be in batch mode) statistics = await this.EnqueueDownloadRequest(statistics, cancellationToken, progress); // Set end time statistics.EndTime = DateTime.Now; // Call EndSession only if BeginSession was successful. if (this.beginSessionComplete) this.localProvider.EndSession(); } catch (OperationCanceledException ex) { statistics.EndTime = DateTime.Now; statistics.Cancelled = true; statistics.Error = ex; this.localProvider.EndSession(); } catch (Exception ex) { statistics.EndTime = DateTime.Now; statistics.Error = ex; this.localProvider.EndSession(); } finally { // Reset the state this.ResetAsyncWorkerManager(); } // Reporting progress if (progress != null) progress.Report(new SyncProgressEvent(SyncStage.EndingSync, statistics.EndTime.Subtract(statistics.StartTime))); return statistics; }
/// <summary> /// Method that synchronize the Cache by uploading all modified changes and then downloading the /// server changes. /// </summary> internal async Task<CacheRefreshStatistics> SynchronizeAsync(CancellationToken cancellationToken, IProgress<SyncProgressEvent> progress = null) { CacheRefreshStatistics statistics = new CacheRefreshStatistics(); // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested(); // set start time statistics.StartTime = DateTime.Now; // Reporting progress if (progress != null) progress.Report(new SyncProgressEvent(SyncStage.StartingSync, TimeSpan.Zero)); try { // First create the CacheRequestHandler this.cacheRequestHandler = new HttpCacheRequestHandler(this.serviceUri, this.controllerBehavior); // Then fire the BeginSession call on the local provider. await this.localProvider.BeginSession(); // Set the flag to indicate BeginSession was successful this.beginSessionComplete = true; // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested(); // Do uploads first (no batch mode on Upload Request) statistics = await this.EnqueueUploadRequest(statistics, cancellationToken, progress); // If there is an error during Upload request, dont want to donwload if (statistics.Error != null) throw new Exception("Error occured during Upload request.", statistics.Error); // Check if cancellation has occured if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested(); // When there are records records that are not uploading successfully // a successfull download 1-N records will cause anchor to be updated. // if anchor is updated then the records on the device will not be attempted for upload again // on subsequent syncs. therefore check for null Error if (statistics.Error == null) { // then Download (be careful, could be in batch mode) statistics = await this.EnqueueDownloadRequest(statistics, cancellationToken, progress); } // Set end time statistics.EndTime = DateTime.Now; // Call EndSession only if BeginSession was successful. if (this.beginSessionComplete) this.localProvider.EndSession(); } catch (OperationCanceledException ex) { statistics.EndTime = DateTime.Now; statistics.Cancelled = true; statistics.Error = ex; if (this.beginSessionComplete) this.localProvider.EndSession(); } catch (Exception ex) { statistics.EndTime = DateTime.Now; statistics.Error = ex; if (this.beginSessionComplete) this.localProvider.EndSession(); } finally { // Reset the state this.ResetAsyncWorkerManager(); } // Reporting progress if (progress != null) progress.Report(new SyncProgressEvent(SyncStage.EndingSync, statistics.EndTime.Subtract(statistics.StartTime))); return statistics; }