/// <summary> /// Creates the manifest async. /// </summary> /// <param name="name">The name.</param> /// <param name="storageAccountName">The name of storage account </param> /// <returns><see cref="Task"/> of type <see cref="IIngestManifest"/></returns> public Task <IIngestManifest> CreateAsync(string name, string storageAccountName) { if (name == null) { throw new ArgumentNullException("name"); } if (storageAccountName == null) { throw new ArgumentNullException("storageAccountName"); } IngestManifestData ingestManifestData = new IngestManifestData { Name = name, StorageAccountName = storageAccountName }; ingestManifestData.SetMediaContext(this.MediaContext); IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext(); dataContext.AddObject(EntitySet, ingestManifestData); MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter); return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(ingestManifestData)) .ContinueWith <IIngestManifest>( t => { t.ThrowIfFaulted(); IngestManifestData data = (IngestManifestData)t.Result.AsyncState; return data; })); }
/// <summary> /// Creates the manifest async. /// </summary> /// <param name="name">The name.</param> /// <param name="storageAccountName">The name of storage account </param> /// <returns><see cref="Task"/> of type <see cref="IIngestManifest"/></returns> public Task <IIngestManifest> CreateAsync(string name, string storageAccountName) { if (name == null) { throw new ArgumentNullException("name"); } if (storageAccountName == null) { throw new ArgumentNullException("storageAccountName"); } IngestManifestData ingestManifestData = new IngestManifestData { Name = name, StorageAccountName = storageAccountName }; ingestManifestData.InitCloudMediaContext(this._cloudMediaContext); DataServiceContext dataContext = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext(); dataContext.AddObject(EntitySet, ingestManifestData); return(dataContext .SaveChangesAsync(ingestManifestData) .ContinueWith <IIngestManifest>( t => { t.ThrowIfFaulted(); IngestManifestData data = (IngestManifestData)t.AsyncState; return data; })); }
/// <summary> /// Creates the manifest asset asyncroniously /// </summary> /// <param name="ingestManifest">The manifest where asset will be defined.</param> /// <param name="asset">The destination asset for which uploaded and processed files will be associated.</param> /// <param name="files">The files which needs to be uploaded and processed.</param> /// <param name="token"><see cref="CancellationToken"/>.</param> /// <returns><see cref="Task"/> of type <see cref="IIngestManifestAsset"/></returns> private Task <IIngestManifestAsset> CreateAsync(IIngestManifest ingestManifest, IAsset asset, string[] files, CancellationToken token) { if (ingestManifest == null) { throw new ArgumentNullException("ingestManifest"); } Action <IngestManifestAssetData> continueWith = (IngestManifestAssetData manifestData) => { Task <IIngestManifestFile>[] tasks = new Task <IIngestManifestFile> [files.Count()]; int i = 0; foreach (string file in files) { token.ThrowIfCancellationRequested(); tasks[i] = ((IIngestManifestAsset)manifestData).IngestManifestFiles.CreateAsync(file, token); i++; } Task continueTask = Task.Factory.ContinueWhenAll( tasks, (fileTasks) => { //Updating statistic var _this = MediaContext.IngestManifests.Where(c => c.Id == _parentIngestManifest.Id).FirstOrDefault(); if (_this != null) { ((IngestManifestData)_parentIngestManifest).Statistics = _this.Statistics; } else { throw new DataServiceClientException(String.Format(CultureInfo.InvariantCulture, StringTable.BulkIngestManifest404, _parentIngestManifest.Id), 404); } List <Exception> exceptions = new List <Exception>(); foreach (Task <IIngestManifestFile> task in fileTasks) { if (task.IsFaulted) { if (task.Exception != null) { exceptions.AddRange(task.Exception.InnerExceptions); } continue; } if (task.IsCanceled) { if (task.Exception != null) { exceptions.Add(task.Exception.Flatten()); } } IngestManifestData ingestManifestData = ((IngestManifestData)ingestManifest); if (task.Result != null) { if (!ingestManifestData.TrackedFilesPaths.ContainsKey(task.Result.Id)) { ingestManifestData.TrackedFilesPaths.TryAdd(task.Result.Id, ((IngestManifestFileData)task.Result).Path); } else { ingestManifestData.TrackedFilesPaths[task.Result.Id] = ((IngestManifestFileData)task.Result).Path; } } else { //We should not be here if task successfully completed and not cancelled exceptions.Add(new NullReferenceException(StringTable.BulkIngestNREForFileTaskCreation)); } } if (exceptions.Count > 0) { var exception = new AggregateException(exceptions.ToArray()); throw exception; } }, TaskContinuationOptions.ExecuteSynchronously); continueTask.Wait(); }; return(CreateAsync(ingestManifest, asset, token, continueWith)); }