WCF学习笔记——契约不能少了set
原创
©著作权归作者所有:来自51CTO博客作者人称左直拳的原创作品,请联系作者获取转载授权,否则将追究法律责任
我定义的WCF契约里,有一个类,里面的属性,有一个因为只读,所以只写了个get。结果客户端就报错。
[DataContract]
public class UserItem
{
public UserItem(int Id, string Name, bool IsActived)
{
this.Id = Id;
this.Name = Name;
this.IsActived = IsActived;
}
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public bool IsActived { get; set; }
[DataMember]
public string BackGround
{
get
{
return IsActived
? "UserItemNull.png"
: "UserItemNullg.png";
}
}
}
刚开始,以为是构造函数的存在,但测试发现不是。
最后发现是最后这个属性"BackGround"没有set!
既然如此,补上一个呗:
[DataContract]
public class UserItem
{
public UserItem(int Id, string Name, bool IsActived)
{
this.Id = Id;
this.Name = Name;
this.IsActived = IsActived;
}
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public bool IsActived { get; set; }
[DataMember]
public string BackGround
{
get
{
return IsActived
? "UserItemNull.png"
: "UserItemNullg.png";
}
private set
{
;
}
}
}
问题解决。