private void DownloadFilesToSyncWithServer(CancellationToken token) { List <UnSyncListModel> lstDataVersions; _eventLog.WriteEntry("Se inicia la descarga de los archivos", EventLogEntryType.Information); using (var repository = new FranchiseRepository()) { lstDataVersions = repository.GetDataVersionsIdsReadyToDownload(); } if (lstDataVersions == null || lstDataVersions.Any() == false) { _eventLog.WriteEntry("No existen archivos para descargar", EventLogEntryType.Information); return; } foreach (var syncListModel in lstDataVersions) { _eventLog.WriteEntry("Descargando version con UID: " + syncListModel.FranchiseDataVersionUid, EventLogEntryType.Information); try { var syncListModelCs = syncListModel; using (var client = new SyncFranchiseClient(new BasicHttpBinding(BasicHttpSecurityMode.None), new EndpointAddress(syncListModel.WsAddress + SettingsData.Constants.Franchise.WS_SYNC_FILES))) { WcfExt.SetMtomEncodingAndSize(client.Endpoint); var tasks = new List <Task>(); using (var repository = new FranchiseRepository()) { var inClient = client; var query = repository.GetFilesToSyncByVersionId(syncListModel.FranchiseDataVersionId); var subscribe = query.ToObservable().Subscribe(fileModel => tasks.Add(DownloadFileAndVerifyCheckSum(syncListModelCs.FranchiseDataVersionUid, fileModel, token, inClient))); Task.WaitAll(tasks.ToArray(), token); subscribe.Dispose(); repository.TrySetFranchiseSyncFilesCompleted(syncListModelCs.FranchiseDataVersionId); } } _eventLog.WriteEntry("Se terminó la descarga de la version con UID: " + syncListModel.FranchiseDataVersionUid, EventLogEntryType.Information); } catch (Exception ex) { _eventLog.WriteEntry(ex.Message + " -ST- " + ex.StackTrace, EventLogEntryType.Error); } } }