为具有“DataContract”属性的类创建的重复属性和“可串行化”
我有一个带有 2 个属性的类,“DataContract”和“Serializable”。此类正在服务中使用。该服务正在被一个项目使用。我使用VS的“添加服务引用”功能在项目中添加了服务的引用(右键单击引用--添加--服务引用)。每当我添加服务的引用时,都会在服务引用的 Reference.cs 文件中创建该类的重复属性,其中 1 个属性具有“DataMember”属性,其他属性具有“Serializable”属性。这会导致构建错误。如果我不添加 Serialized 属性,则服务引用只需使用具有“DataMember”属性的类的 1 组属性即可正常工作。
知道为什么会发生这种情况吗?我们可以采取什么措施来避免这种情况?请注意,这两个属性都是该类所必需的,即我无法删除任何属性。
I have a class with 2 attributes on it, "DataContract" and "Serializable". This class is being used in a service. This service is being consumed by a project. I have added the reference of the service in the project by using the "add service reference" functionality of VS (Right Click on references -- Add -- Service Reference). Whenever I add the reference of the service, duplicate properties of the class is created in the Reference.cs file of the service reference, 1 with "DataMember" attribute and other with "Serializable" attribute. This is causing a build error. If I do not put the Serializable attribute, the service reference works fine with just 1 set of properties of the class with "DataMember" attribute.
Any idea why is this happening and what can we do to avoid this? Please note that both the attributes are required for the class i.e. I cannot remove any of the attributes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能解释一下为什么需要这两个属性吗?
DataContractSerializer
可以与Serialized
属性配合使用。但是,要使
BinaryFormattor
工作,您需要Serialized
属性。找到一种方法来摆脱
DataContract
属性。--编辑--
WCF
默认使用DataContractSerializer
通过通道序列化/反序列化对象。根据MSDN
文档,@ DataContractSerializer 类
因此,为了使 WCF 正常工作,您可以摆脱
DataContract
。Can you explain why you need both the attributes?
DataContractSerializer
works fine withSerializable
attribute.But, for
BinaryFormattor
to work, you needSerializable
attribute.Find a way to get rid of
DataContract
attribute.--EDIT--
WCF
usesDataContractSerializer
by default to serialize/deserialize objects over channels. According toMSDN
documentation,@ DataContractSerializer Class
So, for WCF to work, you can get rid of
DataContract
s.@Samar最好的方法是仅使用 DataContract 属性,因为它在内部使 Serialized 属性进入图片(默认情况下)。
@Samar The best way would be to use only DataContract attribute, as it internally makes the Serializable attribute come in to picture (by default).