WCF and REST endpoint

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

  1. Scott

    Scott Guest

    Hello,

    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.

    Thanks,
    Scott, Jan 29, 2010
    #1
    1. Advertising

  2. Scott

    Steven Cheng Guest

    Hi,

    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.

    [OperationContract]
    [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
    http://msdn.microsoft.com/en-us/library/bb412179.aspx

    Sincerely,

    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
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.




    --------------------
    >From: =?Utf-8?B?U2NvdHQ=?= <>
    >Subject: WCF and REST endpoint
    >Date: Thu, 28 Jan 2010 16:41:01 -0800


    >
    >Hello,
    >
    >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.
    >
    >Thanks,
    >
    >
    >
    Steven Cheng, Jan 29, 2010
    #2
    1. Advertising

  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.

    Thanks,
    Scott
    Scott, Jan 29, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pbd22
    Replies:
    1
    Views:
    1,756
    Mr. Arnold
    Mar 21, 2009
  2. Cindy Lee
    Replies:
    1
    Views:
    2,067
    Mr. Arnold
    Mar 19, 2010
  3. Paul S
    Replies:
    1
    Views:
    1,482
    Mr. Arnold
    Mar 20, 2010
  4. Ryan Liu
    Replies:
    1
    Views:
    747
    Mr. Arnold
    Aug 1, 2010
  5. Larry

    WCF endpoint certificate identity

    Larry, Sep 30, 2008, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    1,398
    Tiago Halm
    Oct 7, 2008
Loading...

Share This Page