Tôi có WCF DataContract sau:Ngoại lệ địa chỉ thu thập dữ liệu. không thể được đăng
[DataContract]
public class Occupant
{
private string _Name;
private string _Email;
private string _Organization;
private string _Badge;
public Occupant(string name, string badge, string organization)
{
Name = name;
Badge = badge;
Organization = organization;
}
public Occupant(string name, string badge)
{
Value = name;
Key = badge;
}
[DataMember]
public string Key
{
get { return _Name; }
set { _Name = value; }
}
[DataMember]
public string Value
{
get { return _Badge; }
set { _Badge = value; }
}
[DataMember]
public string Name
{
get { return _Name; }
set { _Name = value; }
}
[DataMember]
public string Email
{
get { return _Email; }
set { _Email = value; }
}
[DataMember]
public string Organization
{
get { return _Organization; }
set { _Organization = value; }
}
[DataMember]
public string Badge
{
get { return _Badge; }
set { _Badge = value; }
}
}
Khi tôi cố gắng truy cập vào dịch vụ này thông qua trình duyệt web (nó được lưu trữ trên IIS), tôi nhận được lỗi này:
System.Runtime.Serialization.InvalidDataContractException: Type 'MyNamespace.Occupant' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.
Một trong những phương pháp của tôi là trả về một số List
của loại Occupant
. Điều này sẽ gây ra nó?
Mặc dù đã xảy ra lỗi, sự kiện của nó cho dù DCS có thực sự cần bất kỳ nhà xây dựng nào hay không - xem http://msdn.microsoft.com/en-us/magazine/cc163569.aspx và http://stackoverflow.com/ câu hỏi/178645/how-does-wcf-deserialization-instantiate-objects-without-calling-a-constructor – StuartLC
Chúng ta có cần một hàm tạo tham số công khai không? Hoặc constructor privateless private cũng sẽ hoạt động tốt? – VCD
@VCD Hàm tạo tham số riêng tư ít hoạt động. Chỉ cần thử trên .Net Framework 4.5 –