枚举、数据契约和 WCF 问题
我是 WCF 新手,有一个简单的问题...
我的 DataContract 类从其公开的方法之一向使用者返回一个 Enum 类型。
消费者能够看到枚举类型,并实例化该类型的变量。
但是,我没有为服务中的枚举提供 [DataContract] 或 [EnumMember]。
我的问题是,为什么客户端仍然能够看到它? 我认为我必须将其设为 [DataContract] 才能与业务对象一起序列化,不是吗?
I am new to WCF and have a simple question...
My DataContract class returns an Enum type to the consumer from one of it's exposed methods.
The consumer is able to see the enum type, and instantiate variables of that typel.
However, I have not provided a [DataContract] nor [EnumMember]s for the enum in the service.
My question is, why is the client still able to see it??
I thought that I would have had to make it a [DataContract] for it to be serialized along with the business object, no?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的 - 您必须 - 直至 .NET 3.5 SP1。
Microsoft“放松”了规则,现在,如果您不放置任何
[DataContract]
和,
任何东西的属性:它只会简单地序列化所有公共属性和必要的类型。DataContractSerializer
的行为将类似于XmlSerializer
>[DataMember]虽然对于简单的场景来说,这可能是一种“更简单”的方法,但您也失去了对命名空间、排序等的大量控制——所以我更喜欢仍然显式应用这些属性,只是为了清楚地表达(并记录!)我的意图。但在 .NET 3.5 SP1 中,
DataContractSerializer
不再需要和强制执行此操作。Yes - you had to - up to .NET 3.5 SP1.
Microsoft "loosened" the rules and now the
DataContractSerializer
will behave like theXmlSerializer
, if you don't put any[DataContract]
and[DataMember]
attributes on anything: it will just simply serialize all public properties and necessary types.While this might be a "simpler" approach for the simple scenario, you also loose lots of control over namespace, ordering etc. - so I prefer to still apply those attributes explicitly, just to clearly express (and document!) my intent. But with .NET 3.5 SP1, it's no longer required and enforced by the
DataContractSerializer
.如果我们不在任何东西上放置任何[DataContract]和[DataMember]属性,它将自动序列化所有公共属性和必要的类型。此功能从更高版本的.NET3.5 SP1开始支持。
if we don't put any [DataContract] and [DataMember] attributes on anything it will automatically serialize all public properties and necessary types.This feature is supported from higher version of .NET3.5 SP1.