Exemplo n.º 1
0
        /// <summary>
        /// Deletes the streaming endpoint asynchronously.
        /// </summary>
        /// <returns>Task to wait on for operation completion.</returns>
        public override Task DeleteAsync()
        {
            return(((Task <IMediaDataServiceResponse>)base.DeleteAsync())
                   .ContinueWith(t =>
            {
                t.ThrowIfFaulted();

                var operationId = t.Result.Single().Headers[StreamingConstants.OperationIdHeader];

                var operation = AsyncHelper.WaitOperationCompletion(
                    GetMediaContext(),
                    operationId,
                    StreamingConstants.DeleteStreamingEndpointPollInterval);

                var messageFormat = Resources.ErrorDeleteStreamingEndpointFailedFormat;
                string message;

                switch (operation.State)
                {
                case OperationState.Succeeded:
                    return;

                case OperationState.Failed:
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.Failed,
                                            operationId, operation.ErrorMessage);
                    throw new InvalidOperationException(message);

                default:         // can never happen unless state enum is extended
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.InInvalidState,
                                            operationId, operation.State);
                    throw new InvalidOperationException(message);
                }
            }));
        }
        /// <summary>
        /// Asynchronously create a new channel.
        /// </summary>
        /// <param name="options"> Channel creation options </param>
        /// <returns>The channel creation task.</returns>
        public Task <IChannel> CreateAsync(ChannelCreationOptions options)
        {
            var response = CreateChannelAsync(options);

            return(response.ContinueWith <IChannel>(t =>
            {
                t.ThrowIfFaulted();
                string operationId = t.Result.Single().Headers[StreamingConstants.OperationIdHeader];

                IOperation operation = AsyncHelper.WaitOperationCompletion(
                    MediaContext,
                    operationId,
                    StreamingConstants.CreateChannelPollInterval);

                string messageFormat = Resources.ErrorCreateChannelFailedFormat;
                string message;

                switch (operation.State)
                {
                case OperationState.Succeeded:
                    var result = (ChannelData)t.Result.AsyncState;
                    result.Refresh();
                    return result;

                case OperationState.Failed:
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.Failed, operationId, operation.ErrorMessage);
                    throw new InvalidOperationException(message);

                default:     // can never happen unless state enum is extended
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.InInvalidState, operationId, operation.State);
                    throw new InvalidOperationException(message);
                }
            }));
        }
        /// <summary>
        /// Asynchronously updates this instance.
        /// </summary>
        /// <returns>Task to wait on for operation completion.</returns>
        public Task UpdateAsync()
        {
            return(SendUpdateOperationAsync()
                   .ContinueWith(t =>
            {
                t.ThrowIfFaulted();

                IOperation operation = t.Result;

                while (operation.State == OperationState.InProgress)
                {
                    operation = AsyncHelper.WaitOperationCompletion(
                        GetMediaContext(),
                        operation.Id,
                        StreamingConstants.CreateChannelPollInterval);
                }

                string messageFormat = Resources.ErrorUpdateFailedFormat;
                string message;

                switch (operation.State)
                {
                case OperationState.Succeeded:
                    return;

                case OperationState.Failed:
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.Failed, operation.Id, operation.ErrorMessage);
                    throw new InvalidOperationException(message);

                default:         // can never happen unless state enum is extended
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.InInvalidState, operation.Id, operation.State);
                    throw new InvalidOperationException(message);
                }
            }));
        }
        protected Task ExecuteActionAsync(Uri uri, TimeSpan pollInterval, params OperationParameter[] operationParameters)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (GetMediaContext() != null)
                {
                    IMediaDataServiceContext dataContext = GetMediaContext().MediaServicesClassFactory.CreateDataServiceContext();

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

                    var response = retryPolicy.ExecuteAction(() => dataContext.Execute(uri, "POST", operationParameters));

                    if (response.StatusCode == (int)HttpStatusCode.NotFound)
                    {
                        throw new InvalidOperationException("Entity not found");
                    }

                    if (response.StatusCode >= 400)
                    {
                        var code = (HttpStatusCode)response.StatusCode;
                        throw new InvalidOperationException(code.ToString());
                    }

                    if (response.StatusCode != (int)HttpStatusCode.Accepted) // synchronous complete
                    {
                        Refresh();
                        return;
                    }

                    string operationId = response.Headers[StreamingConstants.OperationIdHeader];

                    var operation = AsyncHelper.WaitOperationCompletion(
                        GetMediaContext(),
                        operationId,
                        pollInterval);

                    Refresh();

                    string messageFormat = Resources.ErrorOperationFailedFormat;

                    switch (operation.State)
                    {
                    case OperationState.Succeeded:
                        return;

                    case OperationState.Failed:
                        throw new InvalidOperationException(
                            string.Format(CultureInfo.CurrentCulture, messageFormat, uri.OriginalString, Resources.Failed, operationId));

                    default:
                        throw new InvalidOperationException(
                            string.Format(CultureInfo.CurrentCulture, messageFormat, uri.OriginalString, operation.State, operationId));
                    }
                }
            }));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Deletes the channel asynchronously.
        /// </summary>
        /// <returns>Task to wait on for operation completion.</returns>
        public override Task DeleteAsync()
        {
            if (string.IsNullOrWhiteSpace(Id))
            {
                throw new InvalidOperationException(Resources.ErrorEntityWithoutId);
            }

            IMediaDataServiceContext dataContext = GetMediaContext().MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AttachTo(EntitySetName, this);
            dataContext.DeleteObject(this);

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

            return(retryPolicy.ExecuteAsync(() => dataContext.SaveChangesAsync(this))
                   .ContinueWith(t =>
            {
                t.ThrowIfFaulted();

                string operationId = t.Result.Single().Headers[StreamingConstants.OperationIdHeader];

                IOperation operation = AsyncHelper.WaitOperationCompletion(
                    GetMediaContext(),
                    operationId,
                    StreamingConstants.DeleteChannelPollInterval);

                string messageFormat = Resources.ErrorDeleteChannelFailedFormat;
                string message;

                switch (operation.State)
                {
                case OperationState.Succeeded:
                    return;

                case OperationState.Failed:
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.Failed, operationId, operation.ErrorMessage);
                    throw new InvalidOperationException(message);

                default:         // can never happen unless state enum is extended
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.InInvalidState, operationId, operation.State);
                    throw new InvalidOperationException(message);
                }
            }));
        }