I have WCF service that return Json.
Data contract defined below
[DataContract]
public class OptionData
{
[DataMember]
public string Book { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public string DealId { get; set; }
[DataMember]
public string DeliveryDate { get; set; }
[DataMember]
public string ExpiryDate { get; set; }
}
And Operation Contract defined as follows
[Description("Returns List of Options by user id")]
[WebGet(UriTemplate = "{sessionId}/Application/{applicationId}?start={start}&limit={limit}&page={page}", ResponseFormat = WebMessageFormat.Json)]
public List<OptionData> GetAllTask(string sessionId, string applicationId)
I need to add dynamically new DataMember field to the OptionData class .
What is the best practice to do it ?
If you know you want JSON, you could always control the serialization yourself (see this post) – just return a string of JSON using an existing library.
Another option is to just us
IsRequired = falseif you know all the possible field names.The final alternative is to use the same pattern WCF uses for Forward-Compatible Contracts – just attach all unknown properties to single collection object (
ExtensionData).ExtensionDatais just a dictionary of key/value pairs according to this post. Unfortunately –ExtensionDatais not writable directly. This would be my approach to simulate whatIExtensibleDataObjectis doing…