使用Odata服务并以JSON格式获取结果 [英] Consume Odata Service and get result in JSON

查看:321
本文介绍了使用Odata服务并以JSON格式获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataServiceContext使用odata服务,并希望以json格式返回数据.

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

我正在寻找这样的东西: 使用DataServiceContext进行Odata查询并以json格式获取结果

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.

我碰到"context.Format.usejson"并尝试搜索它,但是没有找到我所能理解的东西.有人可以帮我吗? 使用ODataLib调用WCF数据服务服务操作和JSON

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

我的目标是使用OData Service并使用DataServiceContext以JSON格式获取结果.

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

推荐答案

注意:仅当您的服务的最大协议版本为3或更高时,这些步骤才有效. OData的版本3引入了新的JSON格式,并且WCF Data Services客户端仅支持此JSON格式. (旧的JSON有效负载的顶部是"__metadata""d":{...}.在新的JSON格式中,您会看到"odata.metadata""odata.type"等).

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.)

首先,请确保您拥有5.1版或更高版本的WCF Data Sevrices客户端库(Visual Studio附带了较早的版本)以及该工具的更新版本,该工具可使Visual Studio中的添加服务引用"起作用.

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.

您可以在此处下载最新的工具安装程序: http://www.microsoft.com/zh-CN/download/details.aspx?id=35840 .

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

安装完成后,我建议通过在NuGet程序包管理器控制台中发出以下命令,升级到WCF数据服务客户端的最新版本:

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

升级到最新的客户端库后,您应该可以在客户端中使用JSON了,而不会出现问题.右键单击Visual Studio中的项目,选择添加服务引用",然后输入服务的元数据文档的URL.在v5.1及更高版本中,这将拉低支持JSON所需的服务的完整模型.

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.

添加服务引用"将自动生成DataServiceContext的子类. (您可以通过在Visual Studio的解决方案资源管理器中选择显示所有文件"并展开服务引用后面的代码来查看此生成的代码.)例如,当我针对http://services.odata.org/V3/OData/OData.svc/$metadata执行添加服务引用"时,客户端库生成一个名为DemoService的类.直接使用该派生类而不是DataServiceContext,就可以调用.Format.UseJson().例如:

"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();

这篇关于使用Odata服务并以JSON格式获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆