WCF 是否需要 DataContract 属性 [英] Is DataContract attributes required for WCF
问题描述
我正在编写 WCF 服务和他的客户端.我想在服务和客户端之间发送/接收我的自定义类的对象.
I'm writing WCF service and his client. I want to send/receive objects of my custom classes, between service and client.
我有 3 个模块
- WCF 服务
- WCF 客户端
- 常用类库
WCF 服务和客户端都引用了公共类库.我不想用 DataContract 属性标记我的所有类.
Both WCF service and client have references to common class library. I don't want to mark all my class with DataContract attributes.
所以,我的问题是WCF 需要 DataContract 属性吗?"
So, my question "Is DataContract attributes required for WCF?"
我使用 .NET 4 和 netTcpBinding.
I use .NET 4 and netTcpBinding.
推荐答案
如果我没记错的话(IIRC),如果你不使用正式的数据契约标记,它默认就像一个字段序列化器.这会起作用,但版本不太容易,因为私有更改可能会破坏客户端/服务器.IMO,您应该始终使用 data-contract/data-member 属性正式装饰 WCF 类型.它可以在没有它们的情况下工作,但出于错误的原因(IIRC,最初没有正式标记它没有工作).
If I recall correctly(IIRC), if you don't use formal data-contract markers, it defaults to acting like a field-serializer. This will work, but is less easy to version, since private changes can break the client/server. IMO you should always formally decorate WCF types with the data-contract/data-member attributes. It will work without them, but for the wrong reasons (IIRC, originally it didn't work without formal markers).
这篇关于WCF 是否需要 DataContract 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!