How to ignore properties marked with [IgnoreDataMember] when calling REST service

901 views Asked by At

I am consuming a REST Xml service.

I have all the necessary classes to do this, (supplied by the dev who wrote the service) but at my end I have to save some of the responses to the DB to perform the tasks of the app I am writing. So I have marked some of these classes I need to put in the DB as partial and extended them so that I can inherit from a DbEntity class which specifies an ID property so I can use EF to save them to the DB thus:

public interface IDbEntity
{
    int ID { get; set; }
}

[Serializable]
public class DbEntity : IDbEntity
{
    [IgnoreDataMember]
    [XmlIgnore]
    public int ID { get; set; }
}

the problem I am facing now, is that when the service call is being de-serialized I get the error

Error in line 1 position 113. 'Element' 'ElementName' from namespace '' is not expected. Expecting element '_x003C_ID_x003E_k__BackingField'

I am simply making the call like this:

var response = await client.PostAsXmlAsync<TReq>("Some/API/Call", req);
TResp val = await msg.Content.ReadAsAsync<TResp>(response)

all the properties in the original classes have Orders specified with their DataMember attributes and I have clearly marked my DB properties to be Ignored, but to no avail.

is there any way I can get this to work? - ie getting the DataContractSerializer to actually ignore the properties I have marked to be ignored when de-serializing?

as an aside, these ignored properties are also being passed to the service when making a call - does IgnoreDataMember actually do anything?

1

There are 1 answers

0
nat On BEST ANSWER

seems that the way to do this is like this

public interface IDbEntity
{
    int ID { get; set; }
}

[Serializable]
[DataContract]
public class DbEntity : IDbEntity
{
    [XmlIgnore]
    public int ID { get; set; }
}

so basically adding the DataContract Attribute but omitting the DataMember attribute on the item you don't want

don't know how I missed that first time around. seems its opt in rather than opt out in this instance.