87

我在我的解决方案中创建了一个 WCF 服务库项目,并且有对此的服务引用。我使用类库中的服务,因此除了类库之外,我还有来自我的 WPF 应用程序项目的引用。服务是直接设置的 - 仅更改以获得异步服务功能。

一切正常 - 直到我想更新我的服务参考。它失败了,所以我最终回滚并重试,但即使那样它也失败了!所以 - 更新服务引用失败而不对其进行任何更改。为什么?!

我得到的错误是这个:

Custom tool error: Failed to generate code for the service reference 
'MyServiceReference'.  Please check other error and warning messages for details.   

该警告提供了更多信息:

Custom tool warning: Cannot import wsdl:portType
Detail: An exception was thrown while running a WSDL import extension: 
System.ServiceModel.Description.DataContractSerializerMessageContractImporter
Error: List of referenced types contains more than one type with data contract name 'Patient' in  
namespace 'http://schemas.datacontract.org/2004/07/MyApp.Model'. Need to exclude all but one of the 
following types. Only matching types can be valid references: 
"MyApp.Dashboard.MyServiceReference.Patient, Medski.Dashboard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" (matching)
"MyApp.Model.Patient, MyApp.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" (matching)
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='ISomeService']

也有两个类似的警告说:

Custom tool warning: Cannot import wsdl:binding
Detail: There was an error importing a wsdl:portType that the wsdl:binding is dependent on.
XPath to wsdl:portType: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='ISomeService']
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:binding[@name='WSHttpBinding_ISomeService']  

同样适用于:

Custom tool warning: Cannot import wsdl:port .. 

我觉得这一切都令人困惑。我在客户端仪表板上没有 Patient 类,除了我通过服务参考获得的那个。那么这是什么意思呢?为什么会突然出现?记住:我什至没有改变任何东西!

现在,在这里找到了解决方案,但没有解释这意味着什么。所以; 在服务的“配置服务引用”中,我取消选中“在引用的程序集中重用类型”复选框。现在重建一切正常,没有问题。但我真正改变了什么?这会对我的申请产生影响吗?什么时候应该取消选中这个?我确实想重用我设置了 DataContract 的类型,但没有更多。如果没有这个检查,我仍然可以访问那些吗?

4

13 回答 13

157

我在这里找到了答案:http ://www.lukepuplett.com/2010/07/note-to-self-don-let-wcf-svcutil-reuse.html

长话短说:我从Advanced 菜单中取消选中Reuse types in reference assembly


我不知道这是否重要,但我使用的不是 MVC,而是 Web 表单。

于 2011-05-23T12:49:31.883 回答
37

添加服务引用时,可以通过两种方式处理服务使用的类型:

  • 这些类型存储在 dll 中,并且该 dll 被客户端和服务器应用程序引用。
  • 这些类型不在客户端引用的 dll 中。在这种情况下,创建服务引用的工具将在 references.cs 文件中创建类型。

有很多事情可能会出错。我们发现,如果工具崩溃,有时删除服务引用并重新启动会更快。

我们已停止使用服务引用。对于我们可以控制客户端和服务的项目,我们使用此截屏视频中描述的方法。

于 2009-12-10T18:43:56.727 回答
10

我今天也有这个问题。我花了一整天的时间才发现我的错误。希望能帮助到你。

我无法导入的类有一个自定义枚举类型属性。此属性标记为 DataMember,Enum 也标记为 DataContract。到目前为止一切都很好。我只是忘记将每个枚举成员标记为 EnumMember。

所以我改变了

[DataContract]
public enum SortMethodType
{
    Default = 0,
    Popularity = 1,
    ReleaseDate = 2,
    PublishedDate = 3,
    TranslatedTitle = 4,
    OriginalTitle = 5,
    UserRating = 6,
    Duration = 7
}

对此:

[DataContract]
public enum SortMethodType
{
    [EnumMember]
    Default = 0,
    [EnumMember]
    Popularity = 1,
    [EnumMember]
    ReleaseDate = 2,
    [EnumMember]
    PublishedDate = 3,
    [EnumMember]
    TranslatedTitle = 4,
    [EnumMember]
    OriginalTitle = 5,
    [EnumMember]
    UserRating = 6,
    [EnumMember]
    Duration = 7
}

它终于奏效了!

于 2011-05-12T19:30:28.637 回答
8

添加引用时转到高级属性并从清单中删除“System.Window.Browser”,它解决了问题。

于 2010-03-16T18:59:24.177 回答
8

这听起来可能很奇怪,但我通过删除引用,然后关闭 Visual Studio,然后再次重新打开它,最后再次添加引用来修复它。

我认为需要重新启动自定义工具之类的东西。

于 2011-12-03T18:24:55.550 回答
4

当它在另一台开发人员机器上运行时,我经常遇到这个错误。尽管我在我的虚拟机中到处都是完整的管理员,但我尝试关闭 Visual Studio,然后使用“以管理员身份运行”重新打开,它神奇地工作了。

祝你好运。

于 2012-08-22T13:25:47.017 回答
3

在将我的解决方案从 Visual Studio (VS) 2010 升级到 2013 并将每个项目的 .NET Framework 从 4 更改为 4.5.1 后,我收到了警告。我关闭了 VS 并重新打开,警告消失了。

于 2016-04-06T16:09:29.940 回答
1

关闭“引用程序集中的重用类型”的一个缺点是它可能会导致引用不明确的问题。这是由于服务引用在引用 .cs 文件中再次创建了这些对象,而您实现服务的代码可能会从原始命名空间引用它们。

当这种情况发生时,我发现检查“指定引用程序集中的重用类型”很有用,它允许我选择仅具有不明确引用的那些,这样可以快速解决问题。

希望它可以帮助别人。

于 2014-02-05T01:40:19.977 回答
0

If in doubt that your service doesn't have any problems (such as problems with enums, or non-serializable classes as mentioned by others) then try to create a new project with a new reference.

I am using Silverlight 5 and I had tried to delete and recreate the reference several times. The reference.cs file just came up completely empty each time and it had been literally years since I'd created it so trying to figure out what had changed in the service was out of the question.

I noticed that the error contained references to 2.0.5.0. Now I don't even know if this is actually relevant to the Silverlight version, but it made me think of just creating a brand new project and then suddenly everything worked.

Warning 2 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: Could not load file or assembly 'System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified. XPath to Error Source: //wsdl:definitions[@targetNamespace='']/wsdl:port Type[@name='IShoppingCart']

于 2013-07-24T00:02:05.973 回答
0

我正在查看我的项目,并且遇到了同样的问题。事实证明,WCF 与网站上的相同 DLL 的不同版本。网站有较新版本的 DLL,而服务引用的是较旧版本的 DLL。一旦它们全部同步,一切都运行良好。

于 2013-11-21T14:22:39.413 回答
0

我的 WCF 服务接口在一个程序集中,实现在另一个程序集中,服务引用在另一个程序集中,与服务引用的客户端分开。将 DataContract 应用于枚举后,我立即收到错误消息。在我将 EnumMember 应用于枚举的字段后,问题就解决了。

于 2011-08-10T22:17:08.180 回答
0

我遇到了同样的错误。我挣扎了将近一天,试图找出问题所在。对我来说,线索是 VS 发出的警告。它试图对 Yahoo.Yui.Compressor.dll 进行某种映射,这是我几天前添加和删除的库(因为我决定不使用它)。这令人震惊,因为图书馆不在那里,但不知何故它试图引用它。

最后,我从垃圾箱中恢复了这个 dll,然后我可以成功更新我的服务引用。

于 2014-03-11T14:17:38.377 回答
0

对于未来在这里的任何人,我有同样的错误,但由版本问题引起,以两种不同的方式。

我有两个 WCF 服务和两个通过服务引用进行通信的客户端应用程序。我更新了双方的nuget包并尝试更新服务引用并收到此错误。

删除没有帮助。不希望取消选中“重用程序集”,因为我需要重用它们——这就是重点。

最后,有两个不同的问题:

1)我相信第一个问题是视觉工作室缓存问题。我仔细检查了所有参考资料,没有发现任何问题,但仍然报告无法找到该文件的先前版本。我卸载了所有的 nuget 包,重新启动了 Visual Studio,然后重新安装了它们。更新服务参考有效。

2)第二个问题是由依赖问题引起的。我更新了双方的 nuget 包,一切看起来都正确,但是未标记的依赖项不同步。例子:

Package Foo v1 引用 Bar v1。可以在不更新引用的情况下将 Foo 和 Bar 独立更新到 v2。如果您同时安装 Foo 和 Bar v2,服务参考工具将扫描 Foo v2,查看对 Bar v1 的参考,但由于找不到旧版本而失败。仅当您为每个软件包更新 dll 的版本号时,才会正确报告此问题。Visual Studio 和 MSBuild 在构建应用程序时没有问题,但服务引用将很难解决所有问题。

我希望这可以帮助别人。

于 2016-01-15T16:57:27.503 回答