none
Can I use DataContractSerializerOperationFormatter to format a list of parameters from client to server?

    Question

  •  

    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!

    Monday, October 29, 2007 6:40 AM

Answers

  • It'll look something like the following:

     

    Code Block

    HttpWebRequest req;

    // set any headers needed ...

    using (Stream stream = req.GetRequestStream())

    {

    using (XmlWriter w = XmlWriter.Create(stream))

    {

    // depends on the contract

    w.WriteStartElement("PurchaseRequest", "http://yournamespace");

    new DataContractSerializer(typeof(Customer)).WriteObject(w, customer);

    new DataContractSerializer(typeof(Order)).WriteObject(w, order);

    w.WriteEndElement();

    }

    }

     

     

    Monday, October 29, 2007 8:10 PM

All replies

  • It'll look something like the following:

     

    Code Block

    HttpWebRequest req;

    // set any headers needed ...

    using (Stream stream = req.GetRequestStream())

    {

    using (XmlWriter w = XmlWriter.Create(stream))

    {

    // depends on the contract

    w.WriteStartElement("PurchaseRequest", "http://yournamespace");

    new DataContractSerializer(typeof(Customer)).WriteObject(w, customer);

    new DataContractSerializer(typeof(Order)).WriteObject(w, order);

    w.WriteEndElement();

    }

    }

     

     

    Monday, October 29, 2007 8:10 PM
  •  

    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!

    Tuesday, October 30, 2007 2:28 AM