InvalidOperationException and FileNotFoundException with Background Tasks in Windows Phone

There are a few Exceptions Windows Phone throws when a Background Task isn’t correctly configured, sadly they’re very misleading. In this blog post I’ll outline some of the common exceptions you’ll get and how to go about fixing them.

Failure to Run

When you call ‘ScheduledActionService.LaunchForTest’ you may get an InvalidOperationException like so:

InvalidOperationException on LaunchForTest

This Exception means you haven’t added the ExtendedTask info in the WMAppManifest.xml file (found in ‘Properties’). Open the file in the XML editor (right-click on it and select ‘Open With’), and make sure you’ve entered the ExtendedTask info:

<Tasks>
  <DefaultTask  Name ="_default" NavigationPage="MainPage.xaml"/>
  <ExtendedTask Name="BackgroundTask">
    <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="TileUpdateAgent" Source="WP7LiveTileDemo" Type="WP7LiveTileDemo.Agents.TileUpdateAgent" />
  </ExtendedTask>
</Tasks>

The values are as follows:

  • Specifier – The type of background agent, you can only have one of each type
  • Name – Name for this task, can be anything you like
  • Source – The name of the Assembly (without the .dll extension) that contains the agent. If this is wrong you’ll get both InvalidOperationException and FileNotFoundException exceptions
  • Type – The full class name (ie. must include the namespace path too) of the agent you set up in code. If this is wrong you’ll get an InvalidOperationException

InvalidOperationException and/or FileNotFoundException

If anything is wrong with the ExtendedTask declaration you’ll get InvalidOperationException or FileNotFoundException being thrown from Microsoft.Phone.ni.dll, it’ll look something like this:

InvalidOperationException from a BackgroundTask in WP InvalidOperationException stack trace from a BackgroundTask in WP

Make sure to double-check your ExtendedTask XML is perfect, especially the Source and Type attributes.

1 comment

Thx for this, my issue was a wrong type

Leave a Reply