Changes on the configuration of WCF to use JSON

Published Mon, Oct 22 2007 22:26

A long time ago I wrote a post on how to configure a WCF service so that it uses JSON as its serialization format. It seems like things change in the last CTP. I still didn't had the time to look into everything but it seems like now you don't add a <bindings> element in order to use JSON. So, what this means is that you'll need something like this to get JSON:

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="TestBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="jsonBehavior" >
                    <enableWebScript/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="TestBehavior" name="Test">
                <endpoint address="" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="ITest"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
</system.serviceModel>

 

If you compare this with the old post entry, you'll see that you no longer have the <bindings> element and we have also dropped the bindingConfiguration attribute on the <endpoint> element. Another thing I've noticed when I've re-run my old code is that now it seems like the return object is wrapped in another anonymous object which only has one property which points to the element that was returned from your service.

 Still no time to investigate on why this is happening...if anyone knows, please put it in the comments since I won't really have much time to investigate into this...

Update: after receiving feedback from Christian Weyer, I've rerun my samples in a new project created with beta 2. Everything is working ok with this new project, so i think that the problem was with my old project...

Comments

# Christian Weyer said on Tuesday, October 23, 2007 12:04 AM

Actually, the format of either the request or the response is already defined on the contract by leveraging RequestFormat and ResponseFormat properties.

The webScript behavior is just there for compatibility reasons with ASP.NET AJAX and is not needed if you want to have plain JSON support (which is defined on the contract, as said).

Why is it on the contract and not on the binding? Well, the semantics of XML and JSON differ significantly and it is really a decision of the contract modeler - JSON (the infoset, if you like ;)) does not support the full feature set of XML, and this is by design as we all know.

# Luis Abreu said on Tuesday, October 23, 2007 2:40 AM

Hello Christian.

Thanks for the clarification. btw, any comments on the response being "wrapped" on an anonymous objecto which has only one property that points to the returned object? here's an example. suppose i had a class called person which was returned from the method. it has only 2 proeprties: Name and Address. in beta 1, i used to get somethjign like this on the client side:

{ "Name" : "Luis", "Address": "Fx" }

which is the exact representation of my object in JSON.

In the current release, I'm getting something like this:

{ "d":{ "Name" : "Luis", "Address": "Fx" } }

maybe I might be failing to see the obvious, but i'm still not sure on why this is being done...

thanks again.

# Christian Weyer said on Tuesday, October 23, 2007 1:06 PM

Hm, this doesn't happen for me. Pls send me the code cw __AT_ thinktecture.com

# luisabreu said on Tuesday, October 23, 2007 2:54 PM

hello again.

hum...not sure on why but it seems like i was having a problem with my old project which was created with previous beta version of vs. i've copied the files to the a new web site project created with 2008 beta2 and everything is working smoothly (ie, it still receives the the anonymous object with the d property i was talking about but in this version the callback js method gets the correct object). thanks again.

# software configuration management » Changes on the configuration of WCF to use JSON said on Wednesday, October 31, 2007 10:15 AM

Pingback from  software configuration management &raquo; Changes on the configuration of WCF to use JSON

# software configuration management » Changes on the configuration of WCF to use JSON said on Wednesday, October 31, 2007 10:15 AM

Pingback from  software configuration management &raquo; Changes on the configuration of WCF to use JSON

# Eric Hauser said on Saturday, November 03, 2007 9:46 AM

Luis,

I spent a few days stuggling with this as well.  If you are still having issues, I put together an end to end example on getting it working:

http://erichauser.net/?p=33

It would be nice to see some documentation on the serialization model though.  Lots of trial and error in getting that hacked out.

# luisabreu said on Saturday, November 03, 2007 11:28 AM

Great post Eric.

thanks for sharing it...

Leave a Comment

(required) 
(required) 
(optional)
(required)