Is it possible to run background transfer from background audio agent?

303 views Asked by At

I want to run Background file transfer from Background audio agent but I get error with example code which runs correct in foreground app.

Here is example:

string transferFileName = @"http://www.reggaeavenue.com/MP3/leave%20short.mp3";
Uri transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute);

BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);

transferRequest.Method = "GET";

string downloadFile = "result.mp3";
Uri downloadUri = new Uri("shared/transfers/" + downloadFile, UriKind.RelativeOrAbsolute);
transferRequest.DownloadLocation = downloadUri;

transferRequest.Tag = downloadFile;

transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;

try
{
   BackgroundTransferService.Add(transferRequest);
}
catch (InvalidOperationException ex)
{
     MessageBox.Show("Unable to add background transfer request. " + ex.Message);
}
catch (Exception)
{
     MessageBox.Show("Unable to add background transfer request.");
}

On the line with adding transferRequest to BackgroundTransferService I get error:

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Microsoft.Phone.BackgroundTransfer.BackgroundTransferRequest.SubmitHelper()
   at Microsoft.Phone.BackgroundTransfer.BackgroundTransferRequest.Submit()
   at Microsoft.Phone.BackgroundTransfer.BackgroundTransferService.Add(BackgroundTransferRequest request)
   at Project.AudioPlaybackAgent.AudioPlayer.CreateBackgroundTransfer()

So is it possible to run transferm from background agent? How can I fix this? Thanks

1

There are 1 answers

3
Romasz On

According to MSDN some API's (including background transfer) are not supported in background agents. Even if you manage to do some things, your app can fail certification tests.

Why not download files in main UI or play it directly from web source?