How to: Enable JSON for Data Services

The OData protocol supports JavaScript Notation (JSON). Additionally, OData provides the $format system query option to allow a client to request the format of the response feed. The system query option, if supported by the service, must override the value of the Accept header of the request.

WCF Data Services supports returning JSON. However, the default WCF Data implementation does not support the $format query option and uses only the value of the Accept header of the request. There cases when your WCF Data service need to support the $format query option, e.g. when the client can’t set the Accept header.

Suppose, you have generated a WCF Data Service based on Telerik Data Access Domain Model by using the Add Telerik Data Access Service wizard.

If you try to run the following query: http://localhost:12687/NorthwindService.svc/Orders?$format=json

You will be presented with this error: The query parameter '$format' begins with a system-reserved '$' character but is not recognized.

To enable support for JSON:

  1. Download the needed code from the Microsoft Code Gallery.
  2. Locate the JSONPSupportBehavior.cs file in the downloaded project and include it in your web project.


  3. Add a reference to System.Runtime.Serialization.dll.
  4. Mark your service with the JSONPSupportBehavior attribute.

    C#

    
        [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
        [JSONPSupportBehavior]
        public partial class NorthwindService : OpenAccessDataService<WebApplication.EntitiesModel>
        {
        }