6

I am consuming odata service using DataServiceContext and want to return data in json format.

I am looking something like this: Odata Query with DataServiceContext and get result as json

If I try to add any request header in the sending request event. I can't see that header in fiddler. Although the event is firing which I have confirmed.

I came across "context.Format.usejson" and try to search it but didn't find anything which I can understand. Can somebody help me please ? Using ODataLib to call a WCF Data Services service operation and JSON

My goal is to consume to OData Service and get result in JSON format using DataServiceContext.

Community
  • 1
  • 1
user2463514
  • 273
  • 1
  • 4
  • 19

2 Answers2

11

Note: These steps only work if max protocol version of your service is 3 or higher. Version 3 of OData introduced a new JSON format, and the WCF Data Services client only supports this JSON format. (Old JSON payloads have things like "__metadata" at the top and "d":{...}. In the new JSON format, you'll see things like "odata.metadata", "odata.type", etc.)

First, make sure you have version 5.1 or greater of the WCF Data Sevrices client library (Visual Studio ships with an older version) and an updated version of the tooling that makes "Add Service Reference" in Visual Studio work.

You can download the latest tools installer here: http://www.microsoft.com/en-us/download/details.aspx?id=35840.

Once you've installed that, I recommend upgrading to the latest version of the WCF Data Services client by issuing the following command in the NuGet package manager console:

Install-Package Microsoft.Data.Services.Client

Once you've upgraded to the latest client library, you should be able to use JSON in the client without a problem. Right click on your project in Visual Studio, select "Add Service Reference" and enter the URL of the metadata document of the service. In v5.1 and above, this will pull down the full model of the service, which is needed to support JSON.

"Add Service Reference" will auto-generate a subclass of DataServiceContext. (You can see this generated code by selecting "Show All Files" in the solution explorer of Visual Studio and expanding the code behind the service reference.) For example, when I do "Add Service Reference" against http://services.odata.org/V3/OData/OData.svc/$metadata, the client library generates a class called DemoService. Use that derived class instead of DataServiceContext directly, and you can just call .Format.UseJson(). For example:

var context = new DemoService(new Uri("http://services.odata.org/V3/OData/OData.svc");
context.Format.UseJson();
Jen S
  • 4,465
  • 1
  • 32
  • 28
  • 1
    Thanks It worked as you said but now I am getting this error... "When you call the UseJson method without a parameter, you must use the LoadServiceModel property to provide a valid IEdmModel instance." – user2463514 Jun 12 '13 at 09:39
  • 1
    Double check that you're using the subclass of DataServiceContext and not DataServiceContext directly. Also make sure you've run the installer I linked to. Are you still seeing the error after doing both steps? – Jen S Jun 12 '13 at 15:30
  • I'm also making the assumption that this is a new-ish project and you can run "Add Service Reference" after you've installed the latest tooling. If you use an old "add service reference" result, you would see that error. – Jen S Jun 12 '13 at 15:55
  • You are suggesting me to install RC ? There is no directly release version ? Please suggest – user2463514 Jun 13 '13 at 07:07
  • Yes this is new project and I can do "Add Service Reference" – user2463514 Jun 13 '13 at 07:25
  • It worked when I installed WCF Data Services tools as you have mentioned. – user2463514 Jun 13 '13 at 09:25
  • Whoops, sorry. I linked to the RC installer instead of RTM. Fixing the link now. – Jen S Jun 13 '13 at 16:34
  • RTM Tool is used for generating metadata reference.cs file and at the time of deployment of application we have to use dlls which are copied in package in the project solution. Is my understanding is correct ? – user2463514 Jun 14 '13 at 15:15
  • @JenS I have installed the 5.3 tooling a few times now and I am still getting the same error: "When you call the UseJson method without a parameter, you must use the LoadServiceModel property to provide a valid IEdmModel instance." I am trying to run the latest tooling against the following service: https://open.jaystack.net/c72e6c4b-27ba-49bb-9321-e167ed03d00b/6494690e-1d5f-418d-adca-0ac515b7b742/api/mydatabase/ What could be going wrong? – t316 Jun 18 '13 at 15:47
  • @t316 What is class are you creating to be your DataServiceContext? Have you made sure it isn't DataServiceContext itself, but rather its auto-generated subclass? – Jen S Jun 18 '13 at 16:14
  • Yes - it is definitely an autogenerated subclass. I think maybe my wcf data services 5.3 tooling install is not properly "taking". In fiddler as a user agent I see: MS Web Services Client Protocol 4.0.30319.18034 - does that mean I am still using the old tool? How do I fix that? – t316 Jun 18 '13 at 16:21
  • I think the user-agent value is unrelated (I think that comes from the HTTP stack that sits underneath the WCF Data Services client library). You can check whether 5.3 is installed or not by going to "Add or remove programs" and checking the version of "WCF Data Services" in that list. Another thought: did you run "Add Service Reference" before or after you ran the 5.3 installer? – Jen S Jun 18 '13 at 16:59
  • In terms of an installation it is definitely a successful one. And I have deleted the generated stub and regenerated multiple times. That's why it is pretty confusing... – t316 Jun 18 '13 at 17:14
  • I tried creating a client against the service you linked to, and I'm seeing the same problem. I'll dig a little and get back to you... – Jen S Jun 18 '13 at 17:34
  • 2
    The issue here is that your service is not a v3 service. The JSON format that the WCF DS client supports is the new v3 JSON, so if your server isn't v3, WCF DS client won't be able to use JSON. From their latest release notes, it seems like jaydata does generally have support for v3, but I'm not sure how you enable it. – Jen S Jun 18 '13 at 17:50
  • Isn't there any way to force the client to use the old style verbose json instead of json light? It seems to me this would be a huge backward compatibility problem...? – t316 Jun 19 '13 at 17:25
  • The WCF data services client has never had support for the old json format. For consumers who need to talk with V2 services, they can still use Atom, so there isn't really a huge backcompat problem (this is simply a feature that this client has never supported). Since the old json format is going away completely in V4 (which is also the version in which OData is getting standardized), I think you're going to see a lot of clients that don't speak the old format. – Jen S Jun 19 '13 at 18:44
  • Thank you - I have also discovered that I can actually post a new record to jaystorm but savechanges chokes on the returned data (which is the inserted record). I think the root d property messes things up? – t316 Jun 19 '13 at 19:42
  • In the old format, "d" is only used for responses. If you're having more trouble, I'd also suggest starting a new stack overflow question. Comment threads can be tough to go into details on :) – Jen S Jun 19 '13 at 21:09
  • Moved my questions to a new post: http://stackoverflow.com/questions/17235141/unable-to-parse-odata-json-responses-containing-root-property-d-using-wcf-clie – t316 Jun 21 '13 at 12:22
  • 1
    Just to emphasize that the _"When you call the UseJson method without a parameter, you must use the LoadServiceModel property to provide a valid IEdmModel instance."_ error is resolved by installing the Tools Installer per the link above. It is _not_ fixed by the latest nuget packages only...easy to overlook. – mdisibio Jan 28 '14 at 18:47
  • @JenS Jen...I know this conversation is a bit old, but that WCF Data Services 5.3.0 RTM Tools upgrade breaks Lightswitch generated code when pointed to an OData source. See [msdn post](http://social.msdn.microsoft.com/Forums/vstudio/en-US/0a07aca5-2aa0-4366-8bc7-06c63c93d4a2/lightswitch-2012-odata-client-after-update-with-wcf-data-services-530-rtm-tools-runtime-error?forum=lightswitch) . Any thoughts? – mdisibio Feb 01 '14 at 23:59
  • Hmm, interesting. I'm not sure what the cause is, but I'll ping some folks who might have a better idea. – Jen S Feb 03 '14 at 05:58
3

You can call context.Format.UseJson method without providing a parameter if you load your service model inside OnContextCreated partial method as shown in the code below:

public partial class DemoService
{
    partial void OnContextCreated()
    {
        this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;
    }
}
Abdul
  • 61
  • 6