/// <summary>
        /// Creates a notification endpoint asynchronously
        /// </summary>
        /// <param name="name">Name of notification endpoint</param>
        /// <param name="endPointType">Notification endpoint type</param>
        /// <param name="endPointAddress">Notification endpoint address</param>
        /// <returns>Task of creating notification endpoint.</returns>
        public Task <INotificationEndPoint> CreateAsync(string name, NotificationEndPointType endPointType,
                                                        string endPointAddress)
        {
            NotificationEndPoint notificationEndPoint = new NotificationEndPoint
            {
                Name            = name,
                EndPointType    = (int)endPointType,
                EndPointAddress = endPointAddress
            };

            notificationEndPoint.SetMediaContext(MediaContext);
            IMediaDataServiceContext dataContext = MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AddObject(NotificationEndPoints, notificationEndPoint);

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

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

                return (NotificationEndPoint)t.Result.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }
        /// <summary>
        /// Create a notification endpoint object in asynchronous mode.
        /// </summary>
        /// <param name="name">Name of notification endpoint</param>
        /// <param name="endPointType">Notification endpoint type</param>
        /// <param name="endPointAddress">Notification endpoint address</param>
        /// <returns>Task of creating notification endpoint.</returns>
        public Task <INotificationEndPoint> CreateAsync(string name, NotificationEndPointType endPointType,
                                                        string endPointAddress)
        {
            NotificationEndPoint notificationEndPoint = new NotificationEndPoint
            {
                Name            = name,
                EndPointType    = (int)endPointType,
                EndPointAddress = endPointAddress
            };

            notificationEndPoint.InitCloudMediaContext(_cloudMediaContext);
            DataServiceContext dataContext = DataContextFactory.CreateDataServiceContext();

            dataContext.AddObject(NotificationEndPoints, notificationEndPoint);

            return(dataContext
                   .SaveChangesAsync(notificationEndPoint)
                   .ContinueWith <INotificationEndPoint>(
                       t =>
            {
                t.ThrowIfFaulted();

                return (NotificationEndPoint)t.AsyncState;
            },
                       TaskContinuationOptions.ExecuteSynchronously));
        }
        /// <summary>
        /// Create a notification endpoint object in asynchronous mode.
        /// </summary>
        /// <param name="name">Name of notification endpoint</param>
        /// <param name="endPointType">Notification endpoint type</param>
        /// <param name="endPointAddress">Notification endpoint address</param>
        /// <param name="credential"></param>
        /// <returns>Task of creating notification endpoint.</returns>
        public Task <INotificationEndPoint> CreateAsync(string name, NotificationEndPointType endPointType,
                                                        string endPointAddress, byte[] credential)
        {
            if (credential == null || credential.Length == 0)
            {
                throw new ArgumentNullException("credential");
            }

            if (endPointType != NotificationEndPointType.WebHook)
            {
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, StringTable.SupportWebHookWithCredentialOnly, "endPointType"));
            }

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

            string protectionKeyId = ContentKeyBaseCollection.GetProtectionKeyIdForContentKey(MediaContext,
                                                                                              ContentKeyType.ConfigurationEncryption);
            X509Certificate2 certToUse = ContentKeyBaseCollection.GetCertificateForProtectionKeyId(MediaContext, protectionKeyId);

            byte[] encryptedContentKey = EncryptionUtils.EncryptSymmetricKeyData(certToUse, credential);

            NotificationEndPoint notificationEndPoint = new NotificationEndPoint
            {
                Name                        = name,
                EndPointType                = (int)endPointType,
                EndPointAddress             = endPointAddress,
                CredentialType              = (int)NotificationEndPointCredentialType.SigningKey,
                EncryptedEndPointCredential = Convert.ToBase64String(encryptedContentKey),
                ProtectionKeyType           = (int)ProtectionKeyType.X509CertificateThumbprint,
                ProtectionKeyId             = protectionKeyId
            };

            notificationEndPoint.SetMediaContext(MediaContext);
            dataContext.AddObject(NotificationEndPoints, notificationEndPoint);

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

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

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