I am working on a REST API that I am posting for using POST and XML as content. In my WebInvoke method, it seems that only a few properties are serialized. For example, I pass FirstName, LastName, Email, Phone and Address, but it does not set the Email and Address properties. Therefore, when it adds data to my database, these fields are empty.
Why is this? Why does he set some properties, but not others? I tried to reinstall the order, and it did not affect anything. This also leads me to another question: should ALL all properties be passed in xml or will only some of them be passed, as I have here? I hope that the answer is not needed, because it can be a fairly dynamic system, and new properties can be added often without changing the xml.
Here are some of the relevant code:
public class Lead { #region Public Properties [DataMember(Name = "LeadId")] public int LeadId { get; set; } [DataMember(Name="FirstName")] public string FirstName { get; set; } [DataMember(Name = "MiddleName")] public string MiddleName { get; set; } [DataMember(Name = "LastName")] public string LastName { get; set; } [DataMember(Name = "Email")] public string Email { get; set; } [DataMember(Name = "Email2")] public string Email2 { get; set; } [DataMember(Name = "Phone")] public string Phone { get; set; } [DataMember(Name = "Phone2")] public string Phone2 { get; set; } [DataMember(Name = "Address")] public string Address { get; set; } [DataMember(Name = "Address2")] public string Address2 { get; set; } [DataMember(Name = "Address3")] public string Address3 { get; set; } [DataMember(Name = "City")] public string City { get; set; } [DataMember(Name = "State")] public string State { get; set; } [DataMember(Name = "Zip")] etc...
Here is OperationContract
[OperationContract] [WebInvoke(Method = "POST", UriTemplate = "leads", BodyStyle= WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] string AddLead(Lead lead);
Here is the AddLead function in the service:
public string AddLead(Lead lead) { string result = lead.Submit(); if (result == "Success") { return "Success. " + lead.LeadId; } else { return result; } }
And here is the XML I pass in:
<?xml version="1.0" encoding="utf-8"?> <Lead xmlns="http://www.myrenamednamespace.com/leads"> <FirstName>John</FirstName> <LastName>Doe</LastName> <Email> JohnDoe@gmail.com </Email> <Phone>8885551234</Phone> <Address>123 Fake St</Address> <City>Fake City</City> </Lead>