private void InsertNew(string[] filePath, bool forAnalize, Encoding encoding) { IsBusy = true; var accountId = Account.Id; var cancellationToken = GetCancellationToken(); var fullLoadTask = Task.Factory.StartNew(() => { var storageClient = new RoyaltyServiceWorker.StorageService.FileServiceClient(); var accountClient = new RoyaltyServiceWorker.AccountService.AccountServiceClient(); var loadFileTasks = storageClient.UploadFiles(filePath, encoding, cancellationToken); //wait upload all files Task.WaitAll(loadFileTasks); var loadFileResult = loadFileTasks.Select(t => t.Result).ToArray(); var newRecord = new ImportQueueRecord() { AccountUID = accountId, FileInfoes = loadFileResult.Select(f => new ImportQueueRecordFileInfo() { ForAnalize = forAnalize, SourceFilePath = f.FileName, Files = (new[] { new ImportQueueRecordFileInfoFile() { FileUID = f.Id } }).ToList() } ).ToList() }; var res = accountClient.PutImportQueueRecord(newRecord); if (res.Error != null) throw new Exception(res.Error); newRecord.CopyObjectFrom(res.Value); return newRecord; }, GetCancellationToken(), TaskCreationOptions.None, TaskScheduler.Default) .ContinueWith(r => { IsBusy = false; if (r.Exception != null) Error = r.Exception.ToString(); localCollection.UpdateCollectionAddOrUpdate(new[] { r.Result }); }, System.Threading.CancellationToken.None, TaskContinuationOptions.AttachedToParent, TaskScheduler.FromCurrentSynchronizationContext()); }
//private Task<bool> SaveTask(ImportQueueRecord item) //{ // var startTask = Task.Factory.StartNew(() => // { // IsBusy = true; // }, GetCancellationToken(), TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); // var clientTask = startTask.ContinueWith((t) => // { // var client = new RoyaltyServiceWorker.AccountService.AccountServiceClient(); // try // { // return item.Id == default(Guid) // ? client.PutImportQueueRecord(item) // : client.UpdateImportQueueRecord(item); // } // finally // { // try { client.Close(); } catch { } // } // }, GetCancellationToken(), TaskContinuationOptions.AttachedToParent, TaskScheduler.Default); // var taskRes = clientTask.ContinueWith((res) => // { // try // { // if (res.Result.Error != null) // throw new Exception(res.Result.Error); // item.CopyObjectFrom(res.Result.Value); // return true; // } // catch(Exception ex) // { // Error = ex.ToString(); // return false; // } // finally // { // IsBusy = false; // } // }, GetCancellationToken(), TaskContinuationOptions.AttachedToParent, TaskScheduler.FromCurrentSynchronizationContext()); // return taskRes; //} private Task<ImportQueueRecord[]> DeleteTask(ImportQueueRecord[] items) { var startTask = Task.Factory.StartNew(() => { IsBusy = true; }, GetCancellationToken(), TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); var clientTask = startTask.ContinueWith((t) => { var client = new RoyaltyServiceWorker.AccountService.AccountServiceClient(); try { return client.RemoveImportQueueRecordRange(items.Select(i => i.Id).ToList()); } finally { try { client.Close(); } catch { } } }, GetCancellationToken(), TaskContinuationOptions.AttachedToParent, TaskScheduler.Default); var taskRes = clientTask.ContinueWith((res) => { try { if (res.Result.Error != null) throw new Exception(res.Result.Error); return items .Where(i => !res.Result.Values.Contains(i.Id)) .ToArray(); } catch (Exception ex) { Error = ex.ToString(); return items; } finally { IsBusy = false; } }, GetCancellationToken(), TaskContinuationOptions.AttachedToParent, TaskScheduler.FromCurrentSynchronizationContext()); return taskRes; }
private Task<bool> SaveTask(RoyaltyServiceWorker.AccountService.AccountDataRecordAdditionalColumn item) { var startTask = Task.Factory.StartNew(() => { IsBusy = true; }, GetCancellationToken(), TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); var clientTask = startTask.ContinueWith((t) => { var client = new RoyaltyServiceWorker.AccountService.AccountServiceClient(); try { return item.Id == default(long) ? client.PutAdditionalColumn(item) : client.UpdateAdditionalColumn(item); } finally { try { client.Close(); } catch { } } }, GetCancellationToken(), TaskContinuationOptions.AttachedToParent, TaskScheduler.Default); var taskRes = clientTask.ContinueWith((res) => { try { if (res.Result.Error != null) throw new Exception(res.Result.Error); item.CopyObjectFrom(res.Result.Value); return true; } catch(Exception ex) { Error = ex.ToString(); return false; } finally { IsBusy = false; } }, GetCancellationToken(), TaskContinuationOptions.AttachedToParent, TaskScheduler.FromCurrentSynchronizationContext()); return taskRes; }