Question

I have a concern in passing complex objects/any other types: because I always get a bad request...Code snippet below:

Service:

[OperationContract]
[WebInvoke(UriTemplate = "saveXML/", Method="POST", BodyStyle= WebMessageBodyStyle.Bare)]
bool saveXML(XElement xmlString)
{
       return true;
}

=========

Client:

private HttpUrlEncodedForm frm = new HttpUrlEncodedForm();

frm.Add("CustomerCode", "123");
frm.Add("CustomerName", "Joseph");
frm.Add("Address", "4th Street Washington Ave. New York");
frm.Add("Country", "United States of America");

using (HttpResponseMessage response = m_RestHttpClient.Post("saveXML/", frm.CreateHttpContent()))
{
   response.EnsureStatusIsSuccessful();
}

or I tried it this way:

var request = new XDocument(
              new XElement("Customer",
              new XElement("CustomerCode", "123"),
              new XElement("CustomerName", "Joseph"),
              new XElement("Address", "4th Street Washington Ave. New York"),
              new XElement("Country", "United States of America")));

 frm.Add("body", request.ToString());

..both ways did not work....this is just a sample i want to use complex types because i will be passing atleast 50 parameters...or if you have any other suggestions feel free to suggest.

Thank you

Best Regards, Ravi

Was it helpful?

Solution 2

Service:

[OperationContract]
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Upload", ResponseFormat= WebMessageFormat.Xml, RequestFormat= WebMessageFormat.Xml)]
public void Upload(Stream data)
{
    StreamReader reader = new StreamReader(data);
    String res = reader.ReadToEnd();
}

=========

Client:

private HttpClient m_RestHttpClient = new HttpClient("http://localhost:17471/CustomerService/");


var form = new HttpUrlEncodedForm();
form.Add("CustomerCode", txtCustomerCode.Text);
form.Add("CustomerName", txtCustomerName.Text);
form.Add("ContactName", txtContactName.Text);
form.Add("Country", txtCountry.Text);
form.Add("PostalCode", txtPostalCode.Text);
form.Add("ClassTemplate", txtClassTemplate.Text); 
form.Add("BusinessType", cmbBusinessType.Text);
form.Add("IsProspect", cmbIsProspect.Text);

using (HttpResponseMessage response = m_RestHttpClient.Post("Upload", frm.CreateHttpContent()))
{
    response.EnsureStatusIsSuccessful();
}

===============================

Output of the text file which was written(by the way this has no limit at all: I can pass as much as many parameters as I want):

CustomerCode=CUST001&CustomerName=Customer+One&ContactName=Fuebo+Gacia&Country=France&PostalCode=8234994&ClassTemplate=Class+Template&BusinessType=Wholesale&IsProspect=True

---Basically my concern now is how to retrieve these values properly I tried passing an xml string but it also had values with different character formats maybe this needs to be parsed or something. Hopefully this helps us solve the issue.

Thanks

OTHER TIPS

You're passing the complex type as an XElement - this is going to make things more complicated. Just pass the strongly-typed object. Let the serializer do the work for you. Plus, you'll get the automatic help page which will show you exactly how you should serialize the XML for your type. Here's another resource for setting up a WCF REST service.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top