我使用复杂类型作为datacontract

时间:2015-08-06 18:45:38

标签: wcf datacontract

我使用复杂类型作为datacontract的datamember。但是如果我将[DataMember]设置为complextype,则该服务会抛出异常。可能是什么原因。 KIndly建议。以下是代码段

[DataContract]
public class GetDetailsFromCardNumberResponseBody
{
    public GetDetailsFromCardNumberResponseBody()
    {
    }

    public GetDetailsFromCardNumberResponseBody
      (PatronAccountCardValidation GetDetailsFromCardNumberResult)
    {
        this.GetDetailsFromCardNumberResult = GetDetailsFromCardNumberResult;
    }

    [DataMember]
    public PatronAccountCardValidation GetDetailsFromCardNumberResult { get; set; }
}

[DataContract(Name="PatronAccountCardValidation")]
public class PatronAccountCardValidation
{
    [DataMember]
    public Patron Patron { get; set; }

    [DataMember]
    public Card Card { get; set; }

    [DataMember]
    public string BonusDollarLocal { get; set; }

    [DataMember]
    public string BonusDollarRemote { get; set; }

    [DataMember]
    public bool Valid { get; set; }

    [DataMember]
    public string MessageText { get; set; }        
}

检查参数PatronAccountCardValidation

检查属性Card。这是一个问题。请帮忙。

收到错误:

  

未处理的类型异常   ' System.ServiceModel.CommunicationException'发生在mscorlib.dll中   附加信息:接收HTTP时发生错误   响应localhost:9090 / DoorAccessAndVendingMachineService。这个   可能是由于服务端点绑定不使用HTTP   协议。这也可能是由于HTTP请求上下文所致   由服务器中止(可能是由于服务关闭)。看到   服务器日志以获取更多详细信息

1 个答案:

答案 0 :(得分:0)

您是否考虑过应用Data Contract Know类型?它允许您事先指定在反序列化期间应包括的类型以供考虑。并且在反序列化期间默认情况下,首先创建未初始化的对象,而不调用该类型上的任何构造函数。然后反序列化所有数据成员。查看更多详情here

[DataContract]
[KnownType(typeof(PatronAccountCardValidation))]
public class GetDetailsFromCardNumberResponseBody
{
    public GetDetailsFromCardNumberResponseBody()
    {
    }

    public GetDetailsFromCardNumberResponseBody
      (PatronAccountCardValidation GetDetailsFromCardNumberResult)
    {
        this.GetDetailsFromCardNumberResult = GetDetailsFromCardNumberResult;
    }

    [DataMember]
    public PatronAccountCardValidation GetDetailsFromCardNumberResult { get; set; }
}

[KnownType(typeof(Card))]
[KnownType(typeof(Patron))]
[DataContract(Name="PatronAccountCardValidation")]
public class PatronAccountCardValidation
{
    [DataMember]
    public Patron Patron { get; set; }

    [DataMember]
    public Card Card { get; set; }

    [DataMember]
    public string BonusDollarLocal { get; set; }

    [DataMember]
    public string BonusDollarRemote { get; set; }

    [DataMember]
    public bool Valid { get; set; }

    [DataMember]
    public string MessageText { get; set; }        
}