I have a simple API scenario where I am having problems with the XmlMediaTypeFormatter. When it tries to serialize my object I get the "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml....." error.
The DTO is properly formed. For example, I can take the object returned in the WebAPI action, create a DataContractSeralizer and serialize just fine. - it only fails when the XmlMediaTypeFormatter does it. It is my understanding it is using the DataContractSeralizer under the covers also.
thanks,
stephen
Here is the DTO collection and model:
[CollectionDataContract(Name="DataGroups",Namespace="")]
[KnownType(typeof(DataGroupModel))]
public class DataGroupCollection : Collection<DataGroupModel>
{
public DataGroupCollection() : base(new List<DataGroupModel>()) { }
public DataGroupCollection(List<DataGroupModel> models) : base(models) { }
}
[DataContract(Name="DataGroup",Namespace="")]
public class DataGroupModel
{
public DataGroupModel() { }
[DataMember]
public int DataGroupId { get; set; }
[DataMember]
public string DataGroupName { get; set; }
[DataMember]
public string DisplayName { get; set; }
[DataMember]
public bool IsEnabled { get; set; }
[DataMember]
public bool IsPublic { get; set; }
[DataMember]
public bool IsSelfOnly { get; set; }
[DataMember]
public bool IsScoped { get; set; }
[DataMember]
public string ScopeProperty { get; set; }
[DataMember]
public string LastUpdatedBy { get; set; }
[DataMember]
public DateTime LastUpdatedDate { get; set; }
}
The DTO is properly formed. For example, I can take the object returned in the WebAPI action, create a DataContractSeralizer and serialize just fine. - it only fails when the XmlMediaTypeFormatter does it. It is my understanding it is using the DataContractSeralizer under the covers also.
thanks,
stephen
Here is the DTO collection and model:
[CollectionDataContract(Name="DataGroups",Namespace="")]
[KnownType(typeof(DataGroupModel))]
public class DataGroupCollection : Collection<DataGroupModel>
{
public DataGroupCollection() : base(new List<DataGroupModel>()) { }
public DataGroupCollection(List<DataGroupModel> models) : base(models) { }
}
[DataContract(Name="DataGroup",Namespace="")]
public class DataGroupModel
{
public DataGroupModel() { }
[DataMember]
public int DataGroupId { get; set; }
[DataMember]
public string DataGroupName { get; set; }
[DataMember]
public string DisplayName { get; set; }
[DataMember]
public bool IsEnabled { get; set; }
[DataMember]
public bool IsPublic { get; set; }
[DataMember]
public bool IsSelfOnly { get; set; }
[DataMember]
public bool IsScoped { get; set; }
[DataMember]
public string ScopeProperty { get; set; }
[DataMember]
public string LastUpdatedBy { get; set; }
[DataMember]
public DateTime LastUpdatedDate { get; set; }
}