VS 2010 错误“对象引用未设置到对象的实例”为WCF服务添加服务引用时

发布于 2024-08-30 02:10:14 字数 333 浏览 11 评论 0原文

我有一个 VS2010 (RTM) 解决方案,其中包含:

WCF 服务项目
控制台WCF客户端项目
DataContracts 和成员的类项目
一些简单类的类项目

我成功在控制台客户端项目中添加了服务引用并运行了客户端。然后,我进行了很长的开发周期,反复修改服务,然后更新控制台服务引用。然后,我使用引用和 app.config 更改了项目以及 .cs 的命名空间和程序集名称。我当然错过了一些事情,因为它无法构建,所以我最终删除了项目引用和服务引用,清理并成功构建。

然后,我尝试再次添加服务引用,它发现了它,但在确定时抛出“对象引用未设置为对象的实例”。修复下面的答案...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

只是偏爱你 2024-09-06 02:10:14

如果您尝试向项目添加服务引用,但同一项目中存在对已卸载的另一个项目的现有服务引用,则会出现此问题。例如,您可以使用以下步骤来重现:

  1. 创建两个独立的 WCF 服务项目,Service1 和 Service2
  2. 创建一个 Client 项目,并添加对 Service1 项目中服务的服务引用
  3. 卸载 Service1 项目
  4. 在客户端项目中,尝试添加对 Service2 项目中的服务的引用 - 您将收到上述错误

虽然这不应该发生,因为服务是独立的,但修复方法是在添加对 Service2 的引用时重新加载 Service1 项目。

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.

‘画卷フ 2024-09-06 02:10:14

发布我自己的修复程序,因为我在其他地方找不到它:

Stackoverflow'ing/Google 搜索没有让我得到任何结果,所以我最终在记事本中打开解决方案文件,发现引用我的控制台项目的条目对 WCF 服务有项目依赖性项目。

删除该 ProjectSection 后,我能够添加服务引用并重新开始工作。

在尝试重现问题时,将服务引用添加到项目会添加服务项目依赖项,但删除服务引用则不会。我认为更改名称空间是罪魁祸首,因为它找不到依赖项引用?

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?

人海汹涌 2024-09-06 02:10:14

服务和客户端之间存在现有的项目依赖关系。
右键单击消费者项目 ->项目依赖关系;
取消选中 Consumer 和 Service 之间的依赖关系。 (这将从使用者中删除任何内部对象或引用)

重试:添加服务引用。

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.

简单爱 2024-09-06 02:10:14

对于遇到此问题且未通过上述方法获得帮助的任何人:
查看 UI 部分是否有一些数据绑定(在我的例子中,它是 silverlight 客户端上的数据网格控件)ymmv
另外,如果您有一个服务客户端,那么这种形式的绑定可能会以 _Completed 方法的形式出现,请仔细查看它,看看您是否缺少一些应该存在的数据/参数。

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

清音悠歌 2024-09-06 02:10:14

从“添加/删除程序”中卸载所有版本的 silver light SDK(Silverlight、Silverlight3 等)和 Silverlight 工具。重新安装 Silverlight 工具,应该可以解决问题。

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.

像极了他 2024-09-06 02:10:14

删除并重新添加服务引用。简单但对我有用。

导致“对象引用”错误的原因只是将服务引用端点 url 从本地更改为我的开发服务器。除了这一次之外,以前从来没有遇到过这个问题。

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.

空袭的梦i 2024-09-06 02:10:14

我在 vs17 中的问题(对象引用未设置为对象的实例):

因此,我在 vs2015 中重命名了该项目,但这并没有重命名该项目所在的文件夹。当我在 vs2017 中打开它并尝试时要添加 WCF 服务,单击“确定”后,我将找不到对象引用。

我的修复:

因此,我从解决方案中卸载了项目文件,并重命名了项目文件夹以匹配项目文件,将项目重新加载到我的 vs2017 解决方案中,现在我可以“添加 WCF 服务”了!

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"!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文