WCF定义的复合类型,在客户端创建时无默认值?
数据契约复合类型
- C# code
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。 [DataContract] public class CompositeType { public CompositeType() { boolValue = true; stringValue = "Hello "; } bool boolValue; string stringValue; [DataMember(EmitDefaultValue=true)] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember(EmitDefaultValue=true)] public string StringValue { get { return stringValue; } set { stringValue = value; } } }
客户端代码:
CompositeType compositeType = new ServiceReference1.CompositeType();
创建实例后compositeType.StringValue 和compositeType.BoolValue 都为空,请问结果为什么会是这样?
是用Microsoft Visual Studio 2010创建的项目
[解决办法]
WCF 无法实现构造传递的,
一:如果你用构造参数,引用时会发生错误.
二:传递的对象是结果,不会构建任何默认值(需要后期设置),具体查看WCF的相关文档.
客户端 服务器端
即:对象->序列化 -> 反序列化-对象