Example #1
0
        /// <summary>
        /// Called when a BackgroundTransferRequest's TransferStatus is set to Completed.
        /// This method will remove the request from the BackgroundTransferService and convert
        /// the result over to a LiveOperationResult and set it on the TaskCompletionSource.
        /// </summary>
        /// <param name="request"></param>
        private void OnTransferStatusComplete(BackgroundTransferRequest request)
        {
            Debug.Assert(request.TransferStatus == TransferStatus.Completed);
            Debug.Assert(BackgroundTransferHelper.IsUploadRequest(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;
                }
            }

            this.OnBackgroundTransferRequestCompleted(request);

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

            if (!BackgroundTransferHelper.IsSuccessfulStatusCode(request.StatusCode))
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"));
                this.tcs.TrySetException(exception);
                return;
            }

            // Once we know we have a *good* upload, we have to send it to the response handler
            // to read it's JSON response body. We are an observer to this class, so it will call us back
            // with its result.
            var responseHandler = new BackgroundUploadResponseHandler(
                request.DownloadLocation,
                this);

            responseHandler.ReadJsonResponseFromDownloadLocation();
        }
        /// <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);
            }
        }