Can I use DataContractSerializerOperationFormatter to format a list of parameters from client to server?
-
Monday, October 29, 2007 6:40 AM
Hi,
About message formatter, from MSDN, we know that WCF uses DataContractSerializer by default to convert between Message and parameters in our web service methods. But from MSDN, DataContractSerializer can only be used for one object at one time. How about a list of parameters? After debugging, I found that DataContractSerializerOperationFormatter or PrimitiveSerializerFormatter is used to do this type of work.
My question is: if we want to use HttpWebRequest to interact with WCF, how can I format the parameters to send to server side?
Thanks so much for your any advice!
All Replies
-
Monday, October 29, 2007 8:10 PMModerator
It'll look something like the following:
Code BlockHttpWebRequest
req;// set any headers needed ...
using
(Stream stream = req.GetRequestStream()){
using (XmlWriter w = XmlWriter.Create(stream)){
// depends on the contractw.WriteStartElement(
"PurchaseRequest", "http://yournamespace"); new DataContractSerializer(typeof(Customer)).WriteObject(w, customer); new DataContractSerializer(typeof(Order)).WriteObject(w, order);w.WriteEndElement();
}
}
-
Tuesday, October 30, 2007 2:28 AM
Thanks, John! Your reply helps me a lot.
But if we use more than one parameters in a [WebInvoke] operation, we have to set BodyType = WebMessageBodyStyle.Wrapped. Then your code above failed! In this case, I want to know that whether DataContractSerializer is the only class used to do serialize or deserialize work? If yes, how can it control the wrapper element and what's the rule? If not, which class should I use to produce a Wrapped Reuqest XML? Thanks again!

