C# WCF 数据协定分部类 - 未选取第二个分部类中的新字段
我有一个 WCF 服务,其中有一些数据契约。我正在使用 Web 服务软件工厂,它使用设计器来创建所有消息和数据以及其他合同,并将它们创建为部分类。当重新生成代码时,将重新创建类。
using WcfSerialization = global::System.Runtime.Serialization;
[WcfSerialization::CollectionDataContract(Namespace = "urn:CAEService.DataContracts", ItemName = "SecurityItemCollection")]
public partial class SecurityItemCollection : System.Collections.Generic.List<SecurityItem>
{
}
数据契约是一个工作正常的自定义类的通用列表。但是,我现在想向此类添加一个属性,因此我在同一命名空间中添加了这个部分类:
public partial class SecurityItemCollection
{
public int TotalRecords { get; set; }
}
这似乎在服务端工作正常,但是当我编译并更新来自客户端的服务引用时,该类不会没有新属性,即当它序列化它并在客户端重新创建它时,它丢失了新属性。有谁知道这是为什么吗?
蒂亚
编辑: 好吧,这正式让我发疯了。我唯一能看到的是它使用的是 CollectionDataContract 属性而不是 DataContract。这是否以某种方式不允许类中的数据成员被序列化?为什么会这样呢?它在服务方面工作正常 - 我可以毫无问题地查看和填充值。但是,当我更新客户端上的服务引用时,什么也没有,只有没有数据成员的集合类。
I have a WCF service in which I have some data contracts. I'm using web service software factory, which uses a designer to create all the message and data and other contracts, and it creates them as partial classes. When the code is regenerated the classes are recreated.
using WcfSerialization = global::System.Runtime.Serialization;
[WcfSerialization::CollectionDataContract(Namespace = "urn:CAEService.DataContracts", ItemName = "SecurityItemCollection")]
public partial class SecurityItemCollection : System.Collections.Generic.List<SecurityItem>
{
}
The data contract is a generic List of a custom class which was working fine. However I now want to add a property to this class, so I added this partial class in the same namespace:
public partial class SecurityItemCollection
{
public int TotalRecords { get; set; }
}
This seems to work fine on the service side, but when I compile and then update the service reference from the client, the class doesn't have the new property i.e. when it serialises it and recreates it on the client side, it's missing the new property. Anyone know why this is?
TIA
EDIT:
Ok this is officially driving me nuts. The only thing I can see is that it is using the CollectionDataContract attribute instead of DataContract. Does this somehow not allow data members in the class to be serialised? Why would that be? It is working fine on the service side - I can see and populate the values no problem. However when I update the service refernce on my client there's nothing, just the colelction class without the data member.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将 DataMember 属性添加到 TotalRecords 属性
Try to add the DataMember attribute to the TotalRecords property
好吧,经过多次搜索,我终于发现这是不允许的,即标记为 CollectionDataContract 的类不能有数据成员。我不知道为什么会这样,但这花了我几个小时,而且很头痛。请参阅以下链接:
WCF CollectionDataContract和数据成员
Ok after much searching I finally found out that this isn't allowed i.e. classes marked as CollectionDataContract can't have data members. Why this is I have no idea but it cost me several hours and a major headache. See link below:
WCF CollectionDataContract and DataMember
我知道这已经很旧了,但当我搜索这个主题时,它不断出现。
我可以通过向属性添加“DataMemberAttribute”属性来实现此功能。下面是一个代码示例。
I know this is old but it kept coming up when I searched on this subject.
I was able to get this working by adding a "DataMemberAttribute" attribute to the property. Below is a code example.