WCF web service Data Members defaulting to null
- by James
I am new to WCF and created a simple REST service to accept an order object (series of strings from XML file), insert that data into a database, and then return an order object that contains the results. To test the service I created a small web project and send over a stream created from an xml doc.
The problem is that even though all of the items in the xml doc get placed into the stream, the service is nullifying some of them when it receives the data. For example lineItemId will have a value but shipment status will show null. I step through the xml creation and verify that all the values are being sent. However, if I clear the datamembers and change the names around, it can work. Any help would be appreciated.
This is the interface code
[ServiceContract]
public interface IShipping
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Orders/UpdateOrderStatus/", BodyStyle=WebMessageBodyStyle.Bare)]
ReturnOrder UpdateOrderStatus(Order order);
}
[DataContract]
public class Order
{
[DataMember]
public string lineItemId { get; set; }
[DataMember]
public string shipmentStatus { get; set; }
[DataMember]
public string trackingNumber { get; set; }
[DataMember]
public string shipmentDate { get; set; }
[DataMember]
public string delvryMethod { get; set; }
[DataMember]
public string shipmentCarrier { get; set; }
}
[DataContract]
public class ReturnOrder
{
[DataMember(Name = "Result")]
public string Result { get; set; }
}
This is what I'm using to send over an Order object:
string lineId = txtLineItem.Text.Trim();
string status = txtDeliveryStatus.Text.Trim();
string TrackingNumber = "1x22-z4r32";
string theMethod = "Ground";
string carrier = "UPS";
string ShipmentDate = "04/27/2010";
XNamespace nsOrders = "http://tempuri.org/order";
XElement myDoc =
new XElement(nsOrders + "Order",
new XElement(nsOrders + "lineItemId", lineId),
new XElement(nsOrders + "shipmentStatus", status),
new XElement(nsOrders + "trackingNumber", TrackingNumber),
new XElement(nsOrders + "delvryMethod", theMethod),
new XElement(nsOrders + "shipmentCarrier", carrier),
new XElement(nsOrders + "shipmentDate", ShipmentDate)
);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:3587/Deposco.svc/wms/Orders/UpdateOrderStatus/");
request.Method = "POST";
request.ContentType = "application/xml";
try
{
request.ContentLength = myDoc.ToString().Length;
StreamWriter sw = new StreamWriter(request.GetRequestStream());
sw.Write(myDoc);
sw.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
XDocument.Parse(responseString).Save(@"c:\DeposcoSvcWCF.xml");
}
}
catch (WebException wEx)
{
Stream errorStream = ((HttpWebResponse)wEx.Response).GetResponseStream();
string errorMsg = new StreamReader(errorStream).ReadToEnd();
}