WCF and REST endpoint

Discussion in 'ASP .Net Web Services' started by Scott, Jan 29, 2010.

  1. Scott

    Scott Guest


    I am working on a project that has a REST endpoint that is created with WCF
    version 3.5. The object that is being returned has the necessary
    [DataControl] & [DataMember] attributes and is serialized to JSON. This is
    working correctly.

    In the code that gets the object, it was found that it is actually already a
    serialized JSON string. When the WCF endpoint was updated to return a
    "string" to prevent the overhead of deserializing the object in the first
    place. The issue is that the serialized JSON string was serialized again and
    created invalid JSON; the client would have to deserialize it twice.

    Is there a way to prevent WCF from serializing the object that is being
    returned so that the string that is already JSON is returned? If this is not
    possible, I'll just return the code to the way it was.

    Scott, Jan 29, 2010
    1. Advertisements

  2. Scott

    Steven Cheng Guest


    From your description, you're developing a WCF rest service which will
    return JSON format data objects, however, you found that the object to
    return has been already in Json =string format, so you're wondering how to
    prevent the data be serialized as Json repeatedly, correct?

    For the data object, is it always pregenerated as Json string before the
    WCF engine serialize it? If so, I think you should define your REST
    service(the serviceContract) to use normal XML response format instead of
    json. e.g.

    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
    ResponseFormat = WebMessageFormat.Xml

    If there is some other objects still need to be serialized as Json
    format(not pregenerated as Json string), you can use
    DataContractJsonSerializer to generate Json format output in code:

    #How to: Serialize and Deserialize JSON Data


    Steven Cheng

    Microsoft MSDN Online Support Lead

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:

    Get notification to my posts through email? Please refer to

    Steven Cheng, Jan 29, 2010
    1. Advertisements

  3. Scott

    Scott Guest

    Hello Steven,

    Thank you for the information.

    Changing the ResponseFormat to XML allows the string to be sent from the
    endpoint within out being serialized. The only issue is that before it is
    deserialized within the client of the endpoint, the XML tags must be removed.
    Ideally there would be a WebMessageFormat of "None" which would do no
    serialization but the solution provided will work.

    Scott, Jan 29, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.