When using Background Agents in Windows Phone you may experience an InvalidOperationException with the message ‘BNS Error: The action request has expired‘ like so:
This error means that your background task either threw an exception or failed to call NotifyComplete(). This error will not go away once you’ve fixed the issue – the background task must be removed. You can either do this in code, or uninstall the app and install it again.
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:
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:
<DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/>
<BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="TileUpdateAgent" Source="WP7LiveTileDemo" Type="WP7LiveTileDemo.Agents.TileUpdateAgent" />
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:
Make sure to double-check your ExtendedTask XML is perfect, especially the Source and Type attributes.
I was recently having a problem with an app closing as soon as it started, with absolutely no error messages at all. Turns out, the ‘Startup object’ value had been cleared, leaving it at the default ‘not set’. Setting it back to the name of the Application sorted it: