添加wcf服务时弹出"an object reference not set to an instance of an object "

serviceref02

I won't bore you with the directors' cut of the past couple of hours. I'll just give you the highlights...

  • Try each of the other services. No luck.
  • Close and reopen solution. No luck.
  • Close and reopen Visual Studio 2010. No luck.
  • Reboot machine. "I'm lying. I wasn't that desperate!"
  • Tried shorter namespaces when adding service references. No luck.
  • Shouting. No luck.
  • Try original service. It worked !

 

Strange. So what was it about this service project that differed from the others ? In fact, they were all slightly different. That was the idea. But they were all good services.

Now each of the services implemented the same contract and so every service I was trying to add a reference to contained the same service name as the one I had added originally.

So I began to think about a possible clash. Was something left behind?

I had removed the service reference and associated code, WSDL and XSD files etc. So that left two possibilities. It was either lurking in the client project file or the solution file. I opened each with Notepad.

Second time lucky. It was in the solution file.

Within the entry for the client project, there was a dependency on something that no longer existed. i.e. the original service project.

serviceref03

I removed the ProjectSection entry and tried once more to add a service reference to an alternate service. It worked!

So my demo now involves me removing this section after showing off each variant of DataContract.

Having found the problem AND the solution, I could have still rewritten it to use multiple endpoints instead but figured it was useful as it stands as it reveals an obscure error that some delegates are likely to encounter when working on something similar. Perhaps some form of brokerage service where during development they need to work with a variety of similar services.

 

简而言之就是你要添加到的项目所依赖的其他项目被unload了。

 

link:http://www.qa.com/about-qa/blogs/2011/august/what-causes-an-object-reference-not-set-to-an-instance-of-an-object-error-when-adding-a-service-reference-to-a-wcf-service/

posted @ 2012-02-09 16:29  fannet  阅读(579)  评论(0编辑  收藏  举报