[英]WCF REST is failing to deserialize class that implements IXMLSerializable
I have WCF project that has been converted to rest.我有已转换为休息的 WCF 项目。 I am having hard time to receiving the right value for a property which its type implements IXMLSerializable
我很难收到其类型实现 IXMLSerializable 的属性的正确值
[Serializable]
[DataContract]
[XmlType(TypeName = "Dimension")]
[XmlInclude(typeof(SharedDimension))]
public class Dimension
{
[DataMember]
public string Id {get;set}
//[DataMember]
[XmlElement(ElementName = "Members")]
public Members Members { get; set; }
// another properties of Dimension class
}
[Serializable]
//[XmlSchemaProvider("GetSchema")]
[XmlSerializerFormat]
[XmlRoot]
public class Members: IXMLSerializable
public List<Member> Items
{
get
{
//code that use the IXMLSerializable goes here
}
set
{
//code that use the IXMLSerializable goes here
}
}
// another properties of Members class
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
[Serializable]
[XmlType(TypeName = "Member")]
public class Member : Equatable<Member>, ICodrObject
{
// properties of Member class
}
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/ProcessDimensions")]
bool ProcessDimensions(int productId, List<Dimension> dimensions);
// another end points
}
public class Service implements IService
{
bool ProcessDimensions(int productId, List<Dimension> dimensions){
// code goes here
}
Calling this endd point from post man, productId,Dimension.Id has the right value but Dimension.Members is NULL从邮递员那里调用此端点,productId,Dimension.Id 具有正确的值,但 Dimension.Members 为 NULL
{
"productId": 000000,
"dimensions": [{
"Id": "D1",
"Members": {
"ID": "M1.1",
"Items": [{
"Id": 1
}]
}
},
{
"Id": "D2",
"Members": {
"ID": "M2.1",
"Items": [{
"Id": 1
}]
}
}]
}
I am still using .NET Framework 4.6.1我仍在使用 .NET Framework 4.6.1
There is nothing wrong with your JSON
data format.您的
JSON
数据格式没有任何问题。 Uncomment out the [DataMember]
statement will solve the issue.取消注释
[DataMember]
语句将解决该问题。
//[DataMember]
[XmlElement(ElementName = "Members")]
public Members Members { get; set; }
Although you say you are using XmlSerializer
, you are actually using the DataContractSerializer
throughout the service.尽管您说您使用的是
XmlSerializer
,但实际上您是在整个服务中使用DataContractSerializer
。 DataContractSerializer
is the default serializer in WCF, which includes the class which is without decorating with [DataContract]
. DataContractSerializer
是 WCF 中的默认序列化器,它包括没有用[DataContract]
修饰的类。 If we want to manually switch to the XmlSerializer
, we are supposed to use [XmlSerializerFormat]
to decorate the service contract.如果我们想手动切换到
XmlSerializer
,我们应该使用[XmlSerializerFormat]
来装饰服务契约。
For details,欲知详情,
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-the-xmlserializer-class https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-the-xmlserializer-class
Feel free to let me know if there is anything I can help with.如果有什么我可以帮忙的,请随时告诉我。
Updated.更新。
Please remove relevant XMlserializer
attributes, it might interfere with the DataContractSerializer
.请删除相关的
XMlserializer
属性,它可能会干扰DataContractSerializer
。 Please consider the below definition.请考虑以下定义。
[DataContract]
public class Dimension
{
[DataMember]
public string Id { get; set; }
[DataMember]
public Members Members { get; set; }
}
//DataContractSerializer is the serializer by default.
public class Members
{
public string ID { get; set; }
public List<Member> Items { get; set; }
}
public class Member
{
public int Id { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.