Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS 2010 Error “Object reference not set to an instance of an object” when adding Service Reference for WCF Service

I have a VS2010 (RTM) solution which contains:

WCF Service project
Console WCF client project
Class project for DataContracts and members
Class project for some simple classes

I successfully added a service reference in the console client project and ran the client. I then did a long dev cycle repeatedly modifying the service then updating console service reference. I then changed the namespace and assembly names for the projects as well as the .cs using references and app.config. I of course missed some things as it would not build so I eventually removed the project references and the service reference, cleaned and built successfully.

I then attempted to add the service reference again, it discovered it but threw the “Object reference not set to an instance of an object” when OK'ing. Fix in answer below...

like image 522
Andy Avatar asked Apr 22 '10 16:04

Andy


People also ask

How do I fix object reference is not set to an instance of an object?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

How do I change my WCF service reference?

To update a service reference In Solution Explorer, right-click the service reference and then click Update Service Reference.

What does it mean when it says object reference not set to an instance of an object?

new List<string>()” prevents the “Object reference not set to an instance of an object” exception. Some of the most common causes are settings, database calls, or API-type calls not returning expected values. For example, you add a new field to your database and don't populate default values for every record.


2 Answers

This problem will be observed if you try to add a service reference to a project, but there is an existing service reference in the same project, to another project that you have unloaded. For example, you can use the following steps to reproduce:

  1. Create two separate WCF service projects, Service1 and Service2
  2. Create a Client project and add a service reference to a service in the Service1 project
  3. Unload the Service1 project
  4. In the client project, try to add a reference to a service in the Service2 project - you will get the above error

Although this shouldn't happen since the services are independent, the fix is to reload the Service1 project while you add the reference to Service2.

like image 145
Xcalibur Avatar answered Sep 21 '22 14:09

Xcalibur


Posting my own fix as I couldn't find it elsewhere:

Stackoverflow'ing/Googling didn't get me anywhere so I eventually opened the solution file in notepad and found that the entry referencing my console project had a project dependency on the WCF Service project.

After I removed that ProjectSection, I was able to add the service reference and was back in business.

In trying to reproduce the problem, adding the service reference to the project adds the service project dependency but removing the service reference does not. I presume changing the namespace was the culprit with it not finding the dependency reference?

like image 31
Andy Avatar answered Sep 20 '22 14:09

Andy