Inheritance: IMediaProtectionServiceCompletion
 void ServiceRequested(MediaProtectionManager sender, ServiceRequestedEventArgs srEvent)
 {
     serviceCompletionNotifier = srEvent.Completion;
     IPlayReadyServiceRequest serviceRequest = (IPlayReadyServiceRequest)srEvent.Request;
     ViewModelBase.Log(serviceRequest.GetType().Name);
     ProcessServiceRequest(serviceRequest);
 }
        void ProtectionManager_ServiceRequested(MediaProtectionManager sender, ServiceRequestedEventArgs srEvent)
        {
            _serviceCompletionNotifier = srEvent.Completion;
            IPlayReadyServiceRequest serviceRequest = (IPlayReadyServiceRequest)srEvent.Request;

            _requestChain = new RequestChain(serviceRequest);
            _requestChain.LicenseRequestUri = new Uri(LAURL);
            _requestChain.RequestConfigData = this.RequestConfigData;
            _requestChain.FinishAndReportResult(new ReportResultDelegate(HandleServiceRequest_Finished));
        }
        // Method not used in this sample. 
        // This shows the minimal configuration for basic reactive playback.
        public void MinConfig(MediaElement mediaElement)
        {
            var manager = new MediaProtectionManager();
            var props = new Windows.Foundation.Collections.PropertySet();
            props.Add("{F4637010-03C3-42CD-B932-B48ADF3A6A54}", "Windows.Media.Protection.PlayReady.PlayReadyWinRTTrustedInput");
            manager.Properties.Add("Windows.Media.Protection.MediaProtectionSystemIdMapping", props);
            manager.Properties.Add("Windows.Media.Protection.MediaProtectionSystemId", "{F4637010-03C3-42CD-B932-B48ADF3A6A54}");
            manager.Properties.Add("Windows.Media.Protection.MediaProtectionContainerGuid", "{9A04F079-9840-4286-AB92-E65BE0885F95}");

            MediaProtectionServiceCompletion completionNotifer = null;
            manager.ServiceRequested += async (sender, srEvent) =>
            {
                completionNotifer = srEvent.Completion;
                var serviceRequest = (IPlayReadyServiceRequest)srEvent.Request;
               
                ProcessServiceRequest(serviceRequest);
                if (serviceRequest is PlayReadyIndividualizationServiceRequest)
                {
                    var indivRequest = serviceRequest as PlayReadyIndividualizationServiceRequest;
                    await indivRequest.BeginServiceRequest();
                    serviceCompletionNotifier.Complete(true);
                }
                else if (serviceRequest is PlayReadyLicenseAcquisitionServiceRequest)
                {
                    var licenseRequest = serviceRequest as PlayReadyLicenseAcquisitionServiceRequest;
                    //licenseRequest.
                    await licenseRequest.BeginServiceRequest();
                    serviceCompletionNotifier.Complete(true);
                    serviceCompletionNotifier = null;
                }

            };
            mediaElement.ProtectionManager = manager;
        }
        /// <summary>
        /// Invoked to send the Individualization Request 
        /// </summary>
        async Task<bool> ReactiveIndivRequest(PlayReadyIndividualizationServiceRequest IndivRequest, MediaProtectionServiceCompletion CompletionNotifier)
        {
            bool bResult = false;
            Exception exception = null;
            log("ProtectionManager PlayReady Individualization Service Request in progress...");
            try
            {
                await IndivRequest.BeginServiceRequest();
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (exception == null)
                {
                    bResult = true;
                }
                else
                {
                    COMException comException = exception as COMException;
                    if (comException != null && comException.HResult == MSPR_E_CONTENT_ENABLING_ACTION_REQUIRED)
                    {
                        IndivRequest.NextServiceRequest();
                    }
                }
            }
            if (bResult == true)
                log("ProtectionManager PlayReady Individualization Service Request successful");
            else
                log("ProtectionManager PlayReady Individualization Service Request failed");
            if (CompletionNotifier != null) CompletionNotifier.Complete(bResult);
            return bResult;

        }
        /// <summary>
        /// Invoked to acquire the PlayReady License
        /// </summary>
        async void LicenseAcquisitionRequest(PlayReadyLicenseAcquisitionServiceRequest licenseRequest, MediaProtectionServiceCompletion CompletionNotifier, string Url, string ChallengeCustomData)
        {
            bool bResult = false;
            string ExceptionMessage = string.Empty;

            try
            {
                if (!string.IsNullOrEmpty(Url))
                {
                    log("ProtectionManager PlayReady Manual License Acquisition Service Request in progress - URL: " + Url);

                    if (!string.IsNullOrEmpty(ChallengeCustomData))
                    {
                        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                        byte[] b = encoding.GetBytes(ChallengeCustomData);
                        licenseRequest.ChallengeCustomData = Convert.ToBase64String(b, 0, b.Length);
                    }

                    PlayReadySoapMessage soapMessage = licenseRequest.GenerateManualEnablingChallenge();

                    byte[] messageBytes = soapMessage.GetMessageBody();
                    HttpContent httpContent = new ByteArrayContent(messageBytes);

                    IPropertySet propertySetHeaders = soapMessage.MessageHeaders;
                    foreach (string strHeaderName in propertySetHeaders.Keys)
                    {
                        string strHeaderValue = propertySetHeaders[strHeaderName].ToString();

                        // The Add method throws an ArgumentException try to set protected headers like "Content-Type"
                        // so set it via "ContentType" property
                        if (strHeaderName.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
                            httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse(strHeaderValue);
                        else
                            httpContent.Headers.Add(strHeaderName.ToString(), strHeaderValue);
                    }
                    CommonLicenseRequest licenseAcquision = new CommonLicenseRequest();
                    HttpContent responseHttpContent = await licenseAcquision.AcquireLicense(new Uri(Url), httpContent);
                    if (responseHttpContent != null)
                    {
                        Exception exResult = licenseRequest.ProcessManualEnablingResponse(await responseHttpContent.ReadAsByteArrayAsync());
                        if (exResult != null)
                        {
                            throw exResult;
                        }
                        bResult = true;
                    }
                    else
                        ExceptionMessage = licenseAcquision.GetLastErrorMessage();
                }
                else
                {
                    log ("ProtectionManager PlayReady License Acquisition Service Request in progress - URL: " + licenseRequest.Uri.ToString());
                    await licenseRequest.BeginServiceRequest();
                    bResult = true;
                }
            }
            catch (Exception e)
            {
                ExceptionMessage = e.Message;
            }

            if (bResult == true)
                log(!string.IsNullOrEmpty(Url) ? "ProtectionManager Manual PlayReady License Acquisition Service Request successful" :
                    "ProtectionManager PlayReady License Acquisition Service Request successful");
            else
                log(!string.IsNullOrEmpty(Url) ? "ProtectionManager Manual PlayReady License Acquisition Service Request failed: " + ExceptionMessage :
                    "ProtectionManager PlayReady License Acquisition Service Request failed: " + ExceptionMessage);
            CompletionNotifier.Complete(bResult);
        }
        /// <summary>
        /// Reactive individualization is triggered by PlayReady when the DRM is first utilized in a new app.
        /// If individualization was triggered as part a license request then that request will be queued until
        /// the indiv process completes. The serviceCompletionNotifier is utilized to notify the ProtectionManager 
        /// that the next service request can begin.
        /// </summary>
        static public async void ReactiveIndividualization(PlayReadyIndividualizationServiceRequest indivRequest,
                                                                MediaProtectionServiceCompletion serviceCompletionNotifier,
                                                                Action callback = null)
        {
            Exception exception = null;
            try
            {
                // The actual service call. 
                // The Indiv ServiceRequest call cannot be customized like most other ServiceRequests. 
                await indivRequest.BeginServiceRequest();
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (serviceCompletionNotifier != null)
                {
                    // The Complete call will notify listeners that the service call is complete 
                    // and queued service requests can begin.
                    serviceCompletionNotifier.Complete(exception == null);
                }
                ViewModelBase.Log("ReactiveIndividualization::Complete");
            }

            // The callback is only used in the sample to show PlayReadyStatics within the UI on the 
            // completion of the ServiceRequest.
            if (callback != null)
            {
                var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    callback.Invoke();
                });
            }
        }
        /// <summary>
        /// Reactive license acquisition is triggered by PlayReady when the DRM does not have a license for the 
        /// content(KeyID) as part of an active playback attempt.
        /// Service requests can be queued which is useful in scenarios such as root/leaf license and PlayReady Domains 
        /// where one license depends on another. The serviceCompletionNotifier is utilized to notify the ProtectionManager 
        /// that the next service request can begin.
        /// </summary>
        static async public void ReactiveLicenseAcquisition(IPlayReadyLicenseAcquisitionServiceRequest licenseRequest, 
                                                                   MediaProtectionServiceCompletion serviceCompletionNotifier,
                                                                   Action callback = null)
        {
            
            Exception exception = null;
            bool success = false;
            try
            {
                await licenseRequest.BeginServiceRequest();
                success = true;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                if (serviceCompletionNotifier != null)
                {
                    // The Complete call will notify listeners that the service call is complete
                    // and queued service requests can begin.
                    serviceCompletionNotifier.Complete(success);
                    serviceCompletionNotifier = null;
                }
                ViewModelBase.Log("ReactiveLicenseAcquisition::Complete");
            }

            // optional call back to update UI.
            if (callback != null)
            {
                var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    callback.Invoke();
                });

            }
        }
        /// <summary>
        /// Invoked to send the Individualization Request 
        /// </summary>
        async Task<bool> ReactiveIndividualizationRequestAsync(PlayReadyIndividualizationServiceRequest individualizationRequest, MediaProtectionServiceCompletion completionNotifier)
        {
            bool bResult = false;
            Log("ProtectionManager PlayReady Individualization Service Request in progress...");
            try
            {
                await individualizationRequest.BeginServiceRequest();
                bResult = true;
            }
            catch (COMException comException) when (comException.HResult == MSPR_E_CONTENT_ENABLING_ACTION_REQUIRED)
            {
                individualizationRequest.NextServiceRequest();
            }
            catch (Exception)
            {
            }

            if (bResult == true)
                Log("ProtectionManager PlayReady Individualization Service Request successful");
            else
                Log("ProtectionManager PlayReady Individualization Service Request failed");
            if (completionNotifier != null) completionNotifier.Complete(bResult);
            return bResult;

        }