예제 #1
0
        private Task <IIngestManifestAsset> CreateAsync(IIngestManifest ingestManifest, IAsset asset, CancellationToken token, Action <IngestManifestAssetData> continueWith)
        {
            IngestManifestCollection.VerifyManifest(ingestManifest);

            IMediaDataServiceContext dataContext = MediaContext.MediaServicesClassFactory.CreateDataServiceContext();
            var data = new IngestManifestAssetData
            {
                ParentIngestManifestId = ingestManifest.Id
            };


            dataContext.AddObject(IngestManifestAssetCollection.EntitySet, data);
            dataContext.AttachTo(AssetCollection.AssetSet, asset);
            dataContext.SetLink(data, "Asset", asset);

            MediaRetryPolicy retryPolicy = this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy();

            Task <IIngestManifestAsset> task = retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(data))
                                               .ContinueWith <IIngestManifestAsset>(t =>
            {
                t.ThrowIfFaulted();
                token.ThrowIfCancellationRequested();
                IngestManifestAssetData ingestManifestAsset = (IngestManifestAssetData)t.Result.AsyncState;
                continueWith(ingestManifestAsset);
                return(ingestManifestAsset);
            }, TaskContinuationOptions.ExecuteSynchronously);

            return(task);
        }
        /// <summary>
        /// Creates the locator async.
        /// </summary>
        /// <param name="locatorId">The locator Id.</param>
        /// <param name="locatorType">Type of the locator.</param>
        /// <param name="asset">The asset.</param>
        /// <param name="accessPolicy">The access policy.</param>
        /// <param name="startTime">The start time.</param>
        /// <param name="name">The name.</param>
        /// <returns>A function delegate that returns the future result to be available through the Task&lt;ILocator&gt;.</returns>
        public Task <ILocator> CreateLocatorAsync(string locatorId, LocatorType locatorType, IAsset asset, IAccessPolicy accessPolicy, DateTime?startTime, string name = null)
        {
            AccessPolicyBaseCollection.VerifyAccessPolicy(accessPolicy);
            AssetCollection.VerifyAsset(asset);

            AssetData assetData = (AssetData)asset;

            LocatorData locator = new LocatorData
            {
                AccessPolicy = (AccessPolicyData)accessPolicy,
                Asset        = assetData,
                Type         = (int)locatorType,
                StartTime    = startTime,
                Name         = name
            };

            if (locatorId != null)
            {
                locator.Id = LocatorData.NormalizeLocatorId(locatorId);
            }

            locator.SetMediaContext(this.MediaContext);

            IMediaDataServiceContext dataContext = this.MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AttachTo(AssetCollection.AssetSet, asset);
            dataContext.AttachTo(AccessPolicyBaseCollection.AccessPolicySet, accessPolicy);
            dataContext.AddObject(LocatorSet, locator);
            dataContext.SetLink(locator, AccessPolicyPropertyName, accessPolicy);
            dataContext.SetLink(locator, AssetPropertyName, asset);

            MediaRetryPolicy retryPolicy =
                this.MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            return(retryPolicy.ExecuteAsync <IMediaDataServiceResponse>(() => dataContext.SaveChangesAsync(locator))
                   .ContinueWith <ILocator>(
                       t =>
            {
                t.ThrowIfFaulted();

                assetData.InvalidateLocatorsCollection();

                return (LocatorData)t.Result.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }