WCF数据合同问题

时间:2011-09-23 09:31:28

标签: wcf

我有这份数据合同:

[DataContract(Namespace = Constants.ServiceNamespace)]
public enum UserEntityType
{
    User = 0,
    Group = 1,
    IPAddress = 2 
}

[DataContract(Namespace = Constants.ServiceNamespace)]
//[KnownType(typeof(UserEntity))]
public class UserEntity
{
    [DataMember]
    public UserEntityType EntityType;
    [DataMember]
    public string Value;
}

[DataContract(Namespace = Constants.ServiceNamespace)]
public class TemporaryPolicyData
{
    [DataMember]
    public DateTime EndTime;
    [DataMember]
    public string URL;
}

[DataContract(Namespace = Constants.ServiceNamespace)]
//[KnownType(typeof(TemporaryPolicyData))]
//[KnownType(typeof(UserEntity))]
public class TemporaryWhitelistData
{
    [DataMember]
    public UserEntity User;
    [DataMember]
    public TemporaryPolicyData Data;
}

[DataContract(Namespace = Constants.ServiceNamespace)]
//[KnownType(typeof(TemporaryWhitelistData))]
//[KnownType(typeof(UserEntity))]
public class WhitelistPolicyData
{
    [DataMember]
    public IEnumerable<TemporaryWhitelistData> TemporaryData;
    [DataMember]
    public IEnumerable<string> Websites;
    [DataMember]
    public IEnumerable<UserEntity> Users;
}

我收到此错误:

  

未处理的类型异常   发生'System.ServiceModel.CommunicationException'   mscorlib.dll中

     

附加信息:从管道读取错误:   管道已经结束。 (109,0x6d)

这似乎与上面定义的数据契约有关,因为如果我简化它,它会返回数据。

出现此错误的操作方法是:

[OperationContract]
WhitelistPolicyData GetWhitelistPolicy();

1 个答案:

答案 0 :(得分:2)

我认为类型SerializationException必须有更有意义的内部异常。

我唯一看到的是枚举中缺少的[EnumMember]属性。试试这个:

[DataContract]
public enum UserEntityType
{
    [EnumMember]
    User = 0,
    [EnumMember]
    Group = 1,
    [EnumMember]
    IPAddress = 2 
}

有关说明,请参阅the docu on msdn。将[DataContract]放在枚举上时,您还必须指定[EnumMember]属性。