Windows Phone Background Agent - Resolving Dependencies on Unsupported APIs with existing libraries

27 November 2011

If you are building a Windows Phone 7.5 (aka Mango) app and have made use of Background Agents, you may not realise that there are quite heavy restrictions on the api calls you can make. In addition, when under certification static analysis will be understaken to resolve this issue whether or not these calls are made at runtime.

This is bad news if you were hoping to simply create a reference to a shared business logic class library and be done!

Before you start CTRLX/CTRLVing around the place, there is a workaround that may help.

Go to Project Properties > Build and add BACKGROUNDAGENT as a Conditional compilation symbol for your Background Agent project in Visual Studio. This will allow us to identify the background agent project.

Create “linked files” in Visual Studio (Add Existing Item > Add as Link) to the classes you need within other libraries. This means you can pick and choose which dependencies your background agent project needs without a full assembly reference.

As the main Phone project has a reference to your BackgroundAgent you may need to resolve circular dependencies as a result of copying these files into your background agent project if your Phone project already references them. You will need to deal with this at the namespace level as follows. I choose to add in a BackgroundAgent into the namespace.

namespace Client.App.BackgroundAgent.AppService
namespace Client.App.AppService

Lastly, it is quite  possible that within individual classses you are making calls to unsupported apis. Keep re-running the Marketplace Test Kit and apply the BACKGROUNDAGENT compilation directive to block out areas of code and using statements your background agent doesn’t require.

And that’s it! If you are starting on a new app, you will want to think carefully about dependencies your background agents might have and ensure you structure your code to make this process as easy as possible.

Want to get started?

We would love to help with your next app or game, please do get in touch.