简体   繁体   中英

WCF REST is failing to deserialize class that implements IXMLSerializable

I have WCF project that has been converted to rest. I am having hard time to receiving the right value for a property which its type implements 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": 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

There is nothing wrong with your JSON data format. Uncomment out the [DataMember] statement will solve the issue.

//[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. DataContractSerializer is the default serializer in WCF, which includes the class which is without decorating with [DataContract] . If we want to manually switch to the XmlSerializer , we are supposed to use [XmlSerializerFormat] to decorate the service contract.
For details,
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 . 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM