Share-n-dipity

SharePoint serendipity is the effect by which one accidentally discovers something fortunate, especially while looking for something else entirely. In this case, it is the occassional musings, observations, and Ouija board readings about the phabulously

Deciphering NullReferenceException or Binding Reference Cycle Errors in Windows Phone 7.1

Deciphering NullReferenceException or Binding Reference Cycle Errors in Windows Phone 7.1

  • Comments 1
  • Likes

I recently ran across a very, very difficult problem to diagnose and fix, and I couldn't find this issue going on anywhere else so I thought I'd write about it here in case it comes up again for anyone else.  In a Windows Phone 7.1 (i.e. Mango) application, you have a service reference - in my case, a WCF application.  What I was seeing is that frequently yet randomly, it would throw an exception just creating the WCF proxy.  What I mean by that is this line of code:

MyService.MyClass foo = new MyService.MyClass("myConfigSettings");

Would throw an exception.  While that's kind of inherently crazy, what was more puzzling and frustrating about it is that the exception it would throw wasn't always the same.  It would throw one of these two exceptions - no rhyme or reason as to which it would throw when:

  • NullReferenceException - absolutely no details here, no message, no inner exception nothing.
  • A binding reference cycle was detected in your configuration. The following reference cycle must be removed: basicHttpBinding/BasicHttpBinding_IMyAppInterface

One of the (many) nutty things about this, is that the exception would only be thrown when the method was invoked from some code that ran during application start up; invoke that method any other time and there's no problem.  Also, if you just step through the exception handling code and then drag the next line of execution back up to create the proxy instance, it always worked.  There were other really strange and weird things that had varying degrees of success, but what finally kind of tipped it off to me was the fact that in the application start up code, there was another chunk of code that executed and it also created an instance of that WCF proxy and it NEVER failed.  So in trying to figure out why one always worked and one frequently failed, it finally occurred to me to look at how the code was being invoked.  As it turns out, I had two different background threads being created, and each one was running a different chunk of code.  Each thread was both calling different methods that contained code that would instantiate an instance of the WCF proxy.  Well this, as it turns out, was the downfall and cause of this little problem.  By reconfiguring the code so that all the app startup tasks run on a single background thread, the weird exceptions magically disappeared.

It was pure luck of course figuring this out (along with a lot of trial and error) so I thought I would share this little nugget in case you find yourself in a similar predicament.

Comments
  • Awesome !!! I have been debugging a similar issue for quite some time!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment