我是 WCF 的新手,有一个简单的问题......
我的 DataContract 类从其公开的方法之一向消费者返回 Enum 类型。
消费者能够看到枚举类型,并实例化该类型的变量。
但是,我没有为服务中的枚举提供 [DataContract] 或 [EnumMember]。
我的问题是,为什么客户仍然能够看到它?我认为我必须将其设为 [DataContract] 才能与业务对象一起序列化,不是吗?
我是 WCF 的新手,有一个简单的问题......
我的 DataContract 类从其公开的方法之一向消费者返回 Enum 类型。
消费者能够看到枚举类型,并实例化该类型的变量。
但是,我没有为服务中的枚举提供 [DataContract] 或 [EnumMember]。
我的问题是,为什么客户仍然能够看到它?我认为我必须将其设为 [DataContract] 才能与业务对象一起序列化,不是吗?
Yes - you had to - up to .NET 3.5 SP1.
Microsoft "loosened" the rules and now the DataContractSerializer
will behave like the XmlSerializer
, 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 支持此功能。