编写一个简单的 OData 客户端:如何查询服务? [英] Writing a simple OData client: how to query service?

查看:41
本文介绍了编写一个简单的 OData 客户端:如何查询服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整这个例子来创建一个简单的 OData 客户端.在此之前,我在 Visual Studio 中添加了一个服务引用到http://services.odata.org/Northwind/Northwind.svc/".

I am trying to adapt this example to create a simple OData client. Before that, I added a service reference in Visual Studio to "http://services.odata.org/Northwind/Northwind.svc/".

通过这一步,我得到了很多类,比如Alphabetical_list_of_product".但是,例如,如何获取按字母顺序排列的产品列表?

By this step I got many classes like "Alphabetical_list_of_product". But how do I get the alphabetical list of products, for example?

具体来说,在例子中作者刚开始:

Specifically, in the example the author just starts with:

OdataClient.NorthwindOdataService.NorthwindEntities dc = 
    new OdataClient.NorthwindOdataService.NorthwindEntities(
        new Uri("http://services.odata.org/Northwind/Northwind.svc/")); 

但是他从哪里获得 OdataClient.NorthwindOdataService.NorthwindEntities 类?

But where did he get the OdataClient.NorthwindOdataService.NorthwindEntities class from?

我是 Web 服务和 OData 的新手,如果问题含糊,请见谅.

I am new to web services and OData, so apologies if the question is vague.

推荐答案

以下是将服务引用添加到项目后如何使用的示例:

Here is an example of how the service reference can be used after it has been added to the project:

// Create a service context object
// "NorthwindEntities" is the name of the class in the generated service reference that derives DataServiceContext
// The URI in should be the same URI you used to add the service reference
var context = new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc/"));

// As Alphabetical_list_of_products is an entity set, it can be directly called from the context
// Call Execute() finally to send the request to the OData service and materialize the response got to "products"
var products = context.Alphabetical_list_of_products.Execute();

// Iterate through all the products and print "ProductName", which is the name of a property on "Alphabetical_list_of_product" entity
foreach (var product in products)
{
    Console.WriteLine(product.ProductName);
}

由于您不熟悉 OData,建议您从 OData V4 开始.添加服务引用支持 OData 服务的客户端代理生成,最高可达 OData V3.OASIS 委员会上的 OData V4 协议微软OData团队博客.

As you are new to OData, it is recommended that you start from OData V4. Add Service Reference supports client side proxy generation of OData service up to OData V3. The OData V4 protocol on OASIS Comittee and the blog of the OData team of Microsoft can be referred to for details.

这篇关于编写一个简单的 OData 客户端:如何查询服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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