May 16

Scheduled periodic background task won’t start on Windows Phone

I’ve created an Windows Phone app that has a User interface app and background task to do some stuff periodically. The programs seems to run OK, no error messages and also seems to schedule the agent. The strange this is, that the agent never runs and there is no message / error or trace that indicates a problem.

One other detail is that on Settings -> Applications -> “background task” the app is not listed there. If the task should be working OK, than there should be the program name.  So apparently the Windows Phone does not see the periodic agent. There is no error whatsoever, no indication but the agent just does not run at all.

What is the solution that I’ve found so this problem: in order to start the the agent you must add a reference to the agent project in the front-end (user interface) project. Without this reference, the periodic task will never start. You add the reference by doing the following:

  • Go to the solution explorer
  • right click References
  • Add Reference
  • Project
  • Select the agent project from the list

If you run the Windows Phone app now, it should schedule the agent as I intended.

Simple once you know IT.

CD

May 14

Using asynchronous programming (WCF) in a Windows Phone background agent (periodic task).

Sometimes you want to have a background agent that does some stuff for you periodically on the background. If that agent has to consume a WCF (Windows Communications Foundation) service you can run into some troubles, due to the asynchronous programming model that is uses in the Windows Phone.

In my occaisson I wanted to asynchronously add a record a Windows Azure database that is accessible through my WCF service. While debugging everyting seems to be executed correctly, but on thing: the oncompleted event never occured and no record was addedd to the database. But also: no error to debug on.

So what was wrong? When the background agent is run, you have to let the OS know when it is finished. You do that by executing NotifyComplete(). I had this statement right after the statement that called the WCF service. It seems that you have to wait until the service raises the completed event.

So you have to put the NotifyCompleted() statement in the completed event that is raised when the WCF async service returns it result.

Simple once you know IT.

May 09

How to solve “There was no endpoint listening at …” on Windows Phone 7 connecting to Azure WCF service

My WCF Service works fine when I test it with wcftest. All the endpoints are exposed as intended. But when I try to consuming the service on my Windows Phone 7 I get the message:

There was no endpoint listening at http://myservice.cloudapp.net/GeneralApplicationServices.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

InnerException: {“The remote server returned an error: NotFound.”}

Everything in the code looked fine, wcftest is also working OK, so what could be the problem? After extensive searching with no decent result I made a new wp7 app with the same service reference. And guess what? It worked right away.

So what was the difference? After I remmed out all the non relevant code, I was left with basically 4 lines of code:

  1. initialize the context: context = newAppActivityClient();AppActivityClient 
  2.  add the eventhandler for the async service: context.AddCompleted += new EventHandler<AddCompletedEventArgs>(context_AddCompleted)
  3. Handle the Async event: MessageBox.Show(“It worked”)
  4. Call the service: context.AddAsync(Guid.NewGuid(),Guid.NewGuid(),Guid.NewGuid(),567,”Dit is de data2″,10,6)

Still not working. This were exactly the four lines of code in my newly created project that indeed was working very well. So no difference in the source code. I also used a fiex GUID (FD9EDB38-864D-49F2-A8AE-B6439AB3362D), no difference, I examined all other files in the solution….. And there was it, just in front of me:

The solution:

Add the networking capability to the WMAppManifest.xml. The code is:

<Capability Name=”ID_CAP_NETWORKING”/>

Normally this capability is default in the manifest. But I removed it, because the first version of my app didn’t need any networking capabilities.

Simple, once you now IT.

CD

Apr 21

Object reference not set to an instance of an object when start debugging in Visual Studio

I’m building a c# solution with two projects

  • Windows Phone 7.1 (Silverlight)
  • Scheduled Task Agent (c#)

When I start debugging (F5) in Visual Studio 2010 I get the message:

“Object reference not set to an instance of an object”

The solution start and there seems to be nothing wrong with the app. Everything works fine, in spite of the error. Because this is very annoying, I deceided to solve this issue.

What was wrong: I’ve set Multiple Startup Projects on the properties of the solution. The Phone project should be started as well as the Scheduled Task Agent. The last setting is the problem. There is no need to set the Schedule Task Agent to start, since it is referenced by the Windows Phone project (On the WP7 project -> add reference -> Project) and will startup when the WP7 project needs this reference.

The solution: Remote the ScheduledTaskAgent from the Multiple Startup Projects list.

Simple once you know it . . . . . .

CD

 

Apr 20

Windows Phone App starts and closes immediatly in Visual Studio after renaming app / namespace

Hi Guys,

I’ll take you to a shortcut: from 10 hours to 10 seconds: After programming my app, and renaming the namespace and the project my app on my Windows Phone Emulator start and then closes againg. No error, no debugging possible.

As many time this 10 hours search for the solution (than only took 1 hours of programming) I found the solution:

  • Go to the properties of your project (not your solution)
  • Open the Application tab
  • Set the correct startup object (in my case this was empty)

Do to the renaming of my namespace / app this startup object was empty, so there was no object to start the app with.

Microsoft: a simple warning if no startup object was found should be nice.

CD