Consume the Odata service and get the result in JSON

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

I look something like this: Odata Query with a DataServiceContext and get the result as json

If I try to add any request header to the send request event. I do not see this title in the violinist. Despite the fact that this is an event that I have confirmed.

I came across "context.Format.usejson" and tried to find it, but did not find anything that I could understand. Can someone help me? Using ODataLib to Call WCF and JSON Data Services

My goal is to use the OData service and get the result in JSON format using the DataServiceContext.

+6
source share
2 answers

Note These steps only work if the maximum protocol version of your service is 3 or higher. OData version 3 introduced the new JSON format, and the WCF Data Services client only supports this JSON format. (Old JSON downloads 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 that you have version 5.1 or more from the WCF Data Sevrices client library (Visual Studio comes with an older version) and an updated version of the toolkit that makes the Add Service Link link in Visual Studio.

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

Once you have installed this, 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 have upgraded to the latest client library, you can use JSON on the client without any problems. Right-click on your project in Visual Studio, select Add Service Link and enter the URL of the service metadata document. In version 5.1 and higher, this will lead to a complete service model, which is necessary to support JSON.

A β€œadd service link” will automatically generate a subclass of DataServiceContext . (You can see this generated code by selecting "Show all files" in the Visual Studio solution explorer and expanding the code behind the service link.) For example, when I do "Add service link" in http://services.odata.org/V3/OData/OData.svc/$metadata , the client library generates a class called DemoService . Use this derived class instead of the DataServiceContext directly, and you can simply call .Format.UseJson() . For instance:

 var context = new DemoService(new Uri("http://services.odata.org/V3/OData/OData.svc"); context.Format.UseJson(); 
+9
source

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

 public partial class DemoService { partial void OnContextCreated() { this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance; } } 
+3
source

Source: https://habr.com/ru/post/947083/


All Articles