コード例 #1
0
        /// <summary>
        /// Called when a BackgroundTransferRequet's TransferStatus is set to Completed.
        /// This method will remove the request from the BackgroundTransferService and call
        /// the LiveOperationEventArgs event handler.
        /// </summary>
        /// <param name="request">request with a TransferStatus that is set to Completed</param>
        private void OnTransferStatusComplete(BackgroundTransferRequest request)
        {
            Debug.Assert(request.TransferStatus == TransferStatus.Completed);
            Debug.Assert(BackgroundTransferHelper.IsDownloadRequest(request));

            this.OnBackgroundTransferRequestCompleted(request);

            // Remove the transfer request in order to make room in the queue for more transfers.
            // Transfers are not automatically removed by the system.
            // Cancelled requests have already been removed from the system and cannot be removed twice.
            if (!BackgroundTransferHelper.IsCanceledRequest(request))
            {
                try
                {
                    this.backgroundTransferService.Remove(request);
                }
                catch (Exception exception)
                {
                    this.tcs.TrySetException(new LiveConnectException(
                                                 ApiOperation.ApiClientErrorCode,
                                                 ResourceHelper.GetString("BackgroundTransferServiceRemoveError"),
                                                 exception));
                    return;
                }
            }

            if (request.TransferError != null)
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"),
                                                         request.TransferError);

                this.tcs.TrySetException(exception);
            }
            else if (!BackgroundTransferHelper.IsSuccessfulStatusCode(request.StatusCode))
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"));
                this.tcs.TrySetException(exception);
            }
            else
            {
                string jsonResponse = string.Format(JsonResponse, request.DownloadLocation.OriginalString.Replace("\\", "\\\\"));
                var    jsonReader   = new JsonReader(jsonResponse);
                var    jsonObject   = jsonReader.ReadValue() as IDictionary <string, object>;
                var    result       = new LiveOperationResult(jsonObject, jsonResponse);

                this.tcs.TrySetResult(result);
            }
        }
コード例 #2
0
        /// <summary>
        /// Attaches to the BackgroundTransferRequest's TransferStatus change and converts it into using
        /// a Task&lt;Tgt;
        /// </summary>
        /// <param name="request">To attach to.</param>
        /// <returns>Task&lt;LiveOperationResult&gt; converted from BackgroundTransferEventArgs.</returns>
        public Task <LiveOperationResult> ConvertTransferStatusChangedToTask(BackgroundTransferRequest request)
        {
            Debug.Assert(BackgroundTransferHelper.IsDownloadRequest(request));

            if (request.TransferStatus != TransferStatus.Completed)
            {
                request.TransferStatusChanged += this.HandleTransferStatusChanged;
            }
            else
            {
                // If we are working with an already completed request just handle it now.
                this.OnTransferStatusComplete(request);
            }

            return(tcs.Task);
        }
コード例 #3
0
 /// <summary>
 /// Attaches to the request's TransferProgressChanged event.
 /// It then converts these events to LiveOperationProgresses and gives them to the IProgress
 /// interface that the object was constructed with.
 /// NOTE: Call DetachFromCompletedRequest from this instance when the given request is Completed.
 /// </summary>
 /// <param name="request">request to attach to</param>
 public void ConvertTransferProgressChanged(BackgroundTransferRequest request)
 {
     Debug.Assert(BackgroundTransferHelper.IsDownloadRequest(request));
     request.TransferProgressChanged += this.HandleTransferProgressChanged;
 }