public void TestAddNewRequestToBackgroundTransferService()
        {
            var          requestUri                = new Uri("http://apis.live.net/v5.0/me/skydrive");
            var          locationOnDevice          = new Uri("/shared/transfers/new_file.txt", UriKind.RelativeOrAbsolute);
            const string accessToken               = "accessToken";
            var          backgroundTransferService = new MockBackgroundTransferService();

            var builder = new BackgroundDownloadOperation.Builder()
            {
                AccessToken = accessToken,
                BackgroundTransferService = backgroundTransferService,
                DownloadLocationOnDevice  = locationOnDevice,
                RequestUri = requestUri,
                BackgroundTransferPreferences = BackgroundTransferPreferences.AllowCellularAndBattery
            };

            BackgroundDownloadOperation operation = builder.Build();

            operation.ExecuteAsync();

            Assert.IsTrue(
                backgroundTransferService.RequestHasBeenAdded(),
                "A BackgroundTransferRequest was NOT added to the BackgroundTransferService.");
        }
示例#2
0
        /// <summary>
        /// Downloads the resource with the given path to the downloadLocation using the Windows Phone
        /// BackgroundTransferService.
        /// </summary>
        /// <param name="path">Path to the resource to download</param>
        /// <param name="downloadLocation">
        ///     The path to the file that will contain the download resource.
        ///     The downloadLocation must exist in /shared/transfers.
        /// </param>
        /// <param name="ct">a token that is used to cancel the background download operation.</param>
        /// <param name="progress">an object that is called to report the background download's progress.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public async Task <LiveOperationResult> BackgroundDownloadAsync(
            string path,
            Uri downloadLocation,
            CancellationToken ct,
            IProgress <LiveOperationProgress> progress)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UrlInvalid"),
                                               "path");
                if (path == null)
                {
                    throw new ArgumentNullException("path", message);
                }

                throw new ArgumentException(message, "path");
            }

            if (downloadLocation == null)
            {
                throw new ArgumentNullException("downloadLocation");
            }

            string filename = Path.GetFileName(downloadLocation.OriginalString);

            if (string.IsNullOrEmpty(filename))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UriMissingFileName"),
                                               "downloadLocation");
                throw new ArgumentException(message, "downloadLocation");
            }

            if (!BackgroundTransferHelper.IsRootedInSharedTransfers(downloadLocation))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("UriMustBeRootedInSharedTransfers"),
                                               "downloadLocation");
                throw new ArgumentException(message, "downloadLocation");
            }

            if (this.Session == null)
            {
                throw new LiveConnectException(ApiOperation.ApiClientErrorCode, ResourceHelper.GetString("UserNotLoggedIn"));
            }

            Uri requestUri = this.GetResourceUri(path, ApiMethod.Download);

            var builder = new BackgroundDownloadOperation.Builder
            {
                RequestUri = requestUri,
                DownloadLocationOnDevice  = downloadLocation,
                BackgroundTransferService = this.BackgroundTransferService,
                Progress = progress,
                BackgroundTransferPreferences = this.BackgroundTransferPreferences
            };

            if (!this.Session.IsValid)
            {
                LiveLoginResult result = await this.Session.AuthClient.RefreshTokenAsync();

                if (result.Status == LiveConnectSessionStatus.Connected)
                {
                    this.Session = result.Session;
                }
            }

            builder.AccessToken = this.Session.AccessToken;

            BackgroundDownloadOperation operation = builder.Build();

            ct.Register(operation.Cancel);

            return(await operation.ExecuteAsync());
        }