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

跟風遠走 提交于 2019-12-05 22:45:46

问题


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...


回答1:


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.




回答2:


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?




回答3:


There is an existing project dependency between the service and the client. Right Click on Consumer Project -> Project Dependencies; Uncheck the dependency between the Consumer and Service. (This will remove any internal objects or references from the consumer)

Retry: Add service reference.




回答4:


To anyone having this problem and not helped with the above approaches : See if the UI portion has some data bindings (in my case it was a datagrid control on a silverlight client) ymmv Also, if you are having a service client, then the bindings in this form probably may come in the form of _Completed method, have a closer look into it to see if you are missing some data/parameters that ought to be there. hth




回答5:


Uninstall all versions of silver light SDKs (Silverlight, Silverlight3, etc) and Silverlight tools from the "Add/Remove Programs". Reinstall the Silverlight-tools, that should fix the issue.




回答6:


Remove and re-add the service reference. Simple yet worked for me.

What caused the "object reference" error was simply changing the service reference endpoint url from local to my dev server. Never had this problem before except for this one time.




回答7:


My Problem in vs17 (Object reference not set to an instance of an object):

So, I had renamed the Project in vs2015, but that didn't rename the folder that the project was in. When I opened it up in vs2017 and tried to ad a WCF Service, after clicking OK I would get object ref not found.

My Fix:

So, I unloaded the project file from the solution and renamed the project folder to match the project file, reloaded the project into my solution in vs2017, now I am able to "Add WCF Service"!



来源:https://stackoverflow.com/questions/2692457/vs-2010-error-object-reference-not-set-to-an-instance-of-an-object-when-adding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!