解决方案将无法构建,因为它无法从服务生成的类型转换为我的类型

2024-04-29

我有一个 WCF 服务项目,它构建得很好,可以生成可访问的 WSDL,并且 svcutil.exe 不会生成任何错误。

我有一个访问该 Web 服务的“服务管理器”项目,并且我已成功向其中添加了服务引用 ABCService。

第三个项目包含我需要来回传递的所有 POCO 对象 - 用 [DataContract] 和 [DataMember] 属性自由装饰。

当我尝试构建解决方案时,我看到 ABCService 的 Reference.cs 有这样的方法(为了简洁起见,我用 (...) 替换了完整的命名空间):

    public (...).Thing SaveThing((...).Thing objThing) {
        return base.Channel.SaveThing(objThing);
    }

    public (...).myCollectionOfThing0mj5ZrAW GetThings() {
        return base.Channel.GetThings();
    }

第一个方法返回单个 Thing,工作正常 - 但每个 MyCollection 方法都会出现错误:

错误 16 参数 1:无法从 '(...).MyCollection' 转换为 '(...).myCollectionOfThing0mj5ZrAW'

我的集合类按照您的预期进行装饰:

[CollectionDataContract]
public class MyCollection<T> : List<T> where T : BaseType
{
  //  ...
}

我不知道为什么它会生成时髦的“myCollectionOfThing0mj5ZrAW”名称,也不知道为什么从一个到另一个的翻译失败

EDIT 1:我尝试过使用

[CollectionDataContract(Name= "myCollection{0}", ItemName = "{0}")]

装饰我的集合类,我得到了相同的错误,但名称更新了:

错误 12 参数 1:无法从 '(...).myCollection' 转换 到“(...).myCollectionThing”

EDIT 2:

Despite having checked the "Reuse types in referenced assemblies", selected the radio button for "specified referenced assemblies", and checking the box next to my POCO assembly: Screenshot of reusing referenced assemblies

...仍然使用唯一的类名生成服务引用:


正如 @vesan 和 @RitchMelton 在评论中所建议的,似乎类型是在客户端重新生成的,你应该reuse the DataContract.

If you are adding service reference, reuse the type in referenced assemblies by selecting radioreuse types in all references assemblies: enter image description here

另请注意,您可能需要将集合类型更改为System.Collection.Generic.List防止列表更改为数组/

如果您使用 SvcUtil 生成代理,则需要使用/reference:重用DataContract生成引用时的程序集,例如:

svcutil /reference:YourDLL.dll http://localhost/YourService?wsdl

欲了解更多详情/refernce with SvcUtilrefer:

ServiceModel 元数据实用工具 (Svcutil.exe) http://msdn.microsoft.com/en-us/library/aa347733(v=vs.110).aspx

svcutil 排除/重用引用的程序集 https://stackoverflow.com/questions/8786096/svcutil-exlude-reuse-refrenced-assemblies

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

解决方案将无法构建,因为它无法从服务生成的类型转换为我的类型 的相关文章

随机推荐