8

It seems from all that I read and watched, exposing JSON from a WCF Data Service should be as easy as adding the JSONPSupportBehavior attribute to the service class.

The problem is that VS2010 doesn't recognize the JSONPSupportBehavior attribute.

Is there a reference I am missing? It seems like from all the articles, it was supported out of the box.

casperOne
  • 73,706
  • 19
  • 184
  • 253
SevilNatas
  • 85
  • 1
  • 4
  • Download the code [here](http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=DataServicesJSONP&DownloadId=11208). This one actually works! – Stephen Oberauer May 17 '11 at 10:36
  • I don't think that it's included by default yet. See this [link](http://code.google.com/p/dotnet-visualization-datasource/source/browse/trunk/Web+Services/WCFDataService/JSONPSupportBehavior.cs?spec=svn84&r=84) for some example code. – mdm20 Jun 08 '10 at 19:52

3 Answers3

9

WCF Data Services supports JSON out of the box, no need to add attributes or anything.

In order to receive a response in JSON format clients use standard HTTP content type negotiation.

Specifically, they need to include application/json in the accept header of the request.

There are examples in this section of the OData protocol documentation.

These examples show the raw HTTP requests. Different client HTTP APIs have different mechanisms to specify request headers.

The JSONPSupportBehavior attribute is an example of an extension that allows clients to use a URL query string option (i.e. $format=json) in addition to content-type negotiation, and also adds support for "JSONP" (i.e. $callback=[function-name]).

These are useful in situations where you don't control the headers, such as when doing cross-domain access through script tags.

If you want to use the JSONPSupportBehavior you can obtain it here:

http://code.msdn.microsoft.com/DataServicesJSONP

casperOne
  • 73,706
  • 19
  • 184
  • 253
Pablo Castro
  • 1,644
  • 11
  • 11
  • YEs, I had been to the link you supplied but missed the download link which had the class file in it. Thanks – SevilNatas Jun 13 '10 at 10:40
2

Newer versions of WCF Data Services support JSON by default and you must have

Accept: application/json;odata=verbose

in the request header.

Accept: application/json

is no longer sufficient. More info here.

Nate Cook
  • 8,395
  • 5
  • 46
  • 37
0

WCF not only suppors JSON out-of-the-box, but it also supports JSONP out-of-the-box if you use WCF WebApi (preview 6). Get in VS 2010 with NuGet. Once you have it installed, look here for more information.

Community
  • 1
  • 1
Keith Morgan
  • 721
  • 7
  • 17