views:

29

answers:

2

Hello All,

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

+1  A: 

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.

Steve Michelotti
Thanks Steve, But how do I implement this in the client side.. Lets say in a simple windows application. And with regards to the Person entity can I just create this in this manner: public class Customer { public string CustomerCode; public string CustomerName; public string Address; public string Country; }It would be helpful if anyone can show how can I implement this in the client application.Thanks
Ravi
im using WCF REST 4.0, Win App C# ... basically im just trying to make a proof of concept wherein i can pass such parameters without any connection with a real entity or database
Ravi
Yes you can create a class that looks like just that. You're going to have an easier times passing "parameters" *as* an object (a long parameter list will get ugly fast). Also the WCF REST Starter Kits on codeplex has "Paste XML as Types" which will automatically convert and XML fragment which you can use as a client proxy class.
Steve Michelotti
If you are focused completely on parameters, then you can use tokens in your WebGet/WebInvoke attributes. Notice the "id" parameter inside the curly braces matches the id parameter passed to the method.[WebGet(UriTemplate = "Person/{id}")]public Person GetPerson(string id)
Steve Michelotti
Ive tried posting xml but the only thing i get is a bad request. How about the HttpUrlEncodedForm --> can I use this in anyway? It would be better if you can show me a clear example since its very unclear in my mind right now on how can I construct this. Thanks
Ravi
Basically my main goal here is to pass the parameters from a client to a server no matter in which ever format. With regards to your comment about focused completely in parameters...yes i am but i cant use it as tokens since URI string has a limit of about 260 characters, and I have to pass 50++ parameters so its out of sight
Ravi
Right, then passing as parameters to the method are completely not an option. You need to pass a message body. Here's a video focused entirely on the HttpClient: http://channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Consuming-REST-services-with-HttpClient/This series of videos will get you everything you need to know: each video is between 5-15 minutes: http://www.pluralsight-training.net/microsoft/olt/howtovideos.aspx?category=WCF%20RESTA good one to start with is: Building RESTful Services with WCF
Steve Michelotti
Hi Steve I will post a temporary solution below it seems to work.. but my concern is in how to extract these data and if this is a legitimate way to do this... I just discovered this by trial and error.
Ravi
A: 

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

Ravi
Why deal with streams when you can deal with a strongly-typed object which makes the code much simpler to work with?
Steve Michelotti
Ive tried making this with a strongly typed object...but it doesnt work...I would be grateful if you can show me a sample project on how i can implement this.. Ive checked your blog http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx which was helpful, but i believe what is missing is how to implement this with a client and their is no stated URI/Operation Contract/Web Invoke Method.
Ravi