tags:

views:

153

answers:

1

Hi everyone,

I'm writing what I'm referring to as a POJ (Plain Old JSON) WCF web service - one that takes and emits standard JSON with none of the crap that ASP.NET Ajax likes to add to it.

It seems that there are three steps to accomplish this:

  1. Change "enableWebScript" to "webHttp" in the endpoint's tag
  2. Decorate the method with [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
  3. Add an incantation of [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] to the service contract

This is all working OK for me - I can pass in and am being returned nice plain JSON.

If I remove the WebInvoke attribute, then I get XML returned instead, so it is certainly doing what it is supposed to do. But it strikes me as odd that the option to specify JSON output appears here and not in the configuration file. Say I wanted to expose my method as an XML endpoint too - how would I do this? Currently the only way I can see would be to have a second method that does exactly the same thing but does not have WebMethodFormat.Json specified. Then rinse and repeat for every method in my service? Yuck.

Specifying that the output should be serialized to JSON in the attribute seems to be completely contrary to the philosophy of WCF, where the service is implemented is a transport and encoding agnostic manner, leaving the nasty details of how the data will be moved around to the configuration file.

Is there a better way of doing what I want to do? Or are we stuck with this awkward attribute? Or do I not understanding WCF deeply enough?

+1  A: 

I haven't fully tested this out yet, BUT, I took a look at WebMessageFormat in reflector and where it was used in code.

There is a attribute of the webHttp element called defaultOutgoingResponseFormat that can be set to "Json" or "Xml".

    <behaviors>
      <endpointBehaviors>
        <behavior name="ServicesJSONEndpointBehavior">
          <webHttp defaultOutgoingResponseFormat="Json"/>
        </behavior>
</behaviors>

I've run into the same issue and typically crufted work-arounds after searching online without much info.

I'll give it a shot with multiple configured endpointBehaviors and report back.

Ethan J. Brown
Yup, this is the correct answer. I just verified in a WCF project that I'm working on.Of note, the WebScriptEnablingBehavior class has the same property -- but if you try to tack defaultOutgoingResponseFormat="Xml" on to the enableWebScript element, the service will return a 0 byte response. The default is set to "Json" under the hood somewhere and if you override it in config (which doesn't show up in Intellisense btw), then BUST.
Ethan J. Brown
I love you man.
Mikey Cee
Haha... no problem. WCF is a beast to deal with at times.
Ethan J. Brown