【问题标题】:Writing a simple OData client: how to query service?编写一个简单的 OData 客户端:如何查询服务?
【发布时间】:2014-08-21 23:58:01
【问题描述】:

我正在尝试调整 this example 来创建一个简单的 OData 客户端。在此之前,我在 Visual Studio 中将服务引用添加到“http://services.odata.org/Northwind/Northwind.svc/”。

通过这一步,我获得了许多课程,例如“Alphabetical_list_of_product”。但是,例如,我如何获得按字母顺序排列的产品列表?

具体来说,在这个例子中作者只是从以下开始:

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

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

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

【问题讨论】:

  • OdataClient = 到您的客户端应用程序的命名空间(如果您将测试命名为其他内容,那么它将不再称为 OdataClient),NorthwindOdataService 等于添加时为您的服务引用命名的内容对该 URL (services.odata.org/Northwind/Northwind.svc) 的服务引用和 NorthwindEntities = 驻留在该 URL 的架构。我希望这会有所帮助

标签: c# odata


【解决方案1】:

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

// 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。可参考OData V4 protocol on OASIS Comitteeblog of the OData team of Microsoft了解详情。

【讨论】:

  • 谢谢,有帮助。我又明白了几件事。 NorthwindEntities 类在那里,但 Visual Studio 出于某种原因无法识别它,因此造成了混乱。重新启动使其工作。
【解决方案2】:

如果您想要一个使用 OData 服务的客户端,一个不错的选择应该是 OData 代码生成器。您可以先阅读教程http://blogs.msdn.com/b/odatateam/archive/2014/03/12/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx

【讨论】:

  • 我会看看那个。谢谢! :)
猜你喜欢
  • 2016-10-23
  • 2013-07-03
  • 2016-01-18
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多