WCF + REST: Where is the request data?

前端 未结 5 1598
有刺的猬
有刺的猬 2020-12-01 11:23

I\'m currently developing a WCF RESTful service. Within the validation of the POST data, I am throwing exceptions if the request XML does not conform to our business rules.

5条回答
  •  醉酒成梦
    2020-12-01 12:17

    So, if you declare your contract something like:

    [WebInvoke(Method = "POST", UriTemplate = "create", ResponseFormat=WebMessageFormat.Json)]
     int CreateItem(Stream streamOfData);
    

    (you can use XML instead) The streamOfData should be the body of an HTTP POST. You can deserialize it using something like:

     StreamReader reader = new StreamReader(streamId);
     String res = reader.ReadToEnd();
     NameValueCollection coll = HttpUtility.ParseQueryString(res);
    

    It's working like that for us, at least. You may want to use a different approach to get the string into an XMLDocument or something. This works for our JSON posts. Might not be the most elegant solution, but it is working.

    I hope this helps.

    Glenn

提交回复
热议问题