0

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

Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182

1 Answers1

2

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?

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Thanks, I can`t find it. Because I want to cache songs and then play them. And I want cache them from background too. – Libor Zapletal Dec 22 '13 at 11:10
  • For sure you can connect background audio and your main UI, that main UI downloads (in background) files and updates the playlist. Downloading in bacgound agent can be hard, note that you have limited time (30 s) to end invoked state (before sending NotifyComplete()) - if your time runs out, your agent would be killed. – Romasz Dec 22 '13 at 11:23
  • Yeah I can download it in background agent but when user try to use controls nothing happens. First agent must be killed and then new can be created which handles response and this is unacceptable for me. – Libor Zapletal Dec 22 '13 at 12:40