本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/calling-an-odata-service-from-a-net-client

by Mike Wasson+

Download Completed Project+

This tutorial shows how to call an OData service from a C# client application.+

Software versions used in the tutorial

+

In this tutorial, I'll walk through creating a client application that calls an OData service. The OData service exposes the following entities:+

    • Product
    • Supplier
    • ProductRating

+

+

The following articles describe how to implement the OData service in Web API. (You don't need to read them to understand this tutorial, however.)+

+

Generate the Service Proxy

The first step is to generate a service proxy. The service proxy is a .NET class that defines methods for accessing the OData service. The proxy translates method calls into HTTP requests.+

+

Start by opening the OData service project in Visual Studio. Press CTRL+F5 to run the service locally in IIS Express. Note the local address, including the port number that Visual Studio assigns. You will need this address when you create the proxy.+

Next, open another instance of Visual Studio and create a console application project. The console application will be our OData client application. (You can also add the project to the same solution as the service.)+

Note

The remaining steps refer the console project.+

In Solution Explorer, right-click References and select Add Service Reference.+

+

In the Add Service Reference dialog, type the address of the OData service:+

consoleCopy
http://localhost:port/odata

where port is the port number.+

+

For Namespace, type "ProductService". This option defines the namespace of the proxy class.+

Click Go. Visual Studio reads the OData metadata document to discover the entities in the service.+

+

Click OK to add the proxy class to your project.+

+

Create an Instance of the Service Proxy Class

Inside your Main method, create a new instance of the proxy class, as follows:+

C#Copy
using System;
using System.Data.Services.Client;
using System.Linq; namespace Client
{
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:1234/odata/");
var container = new ProductService.Container(uri); // ...
}
}
}

Again, use the actual port number where your service is running. When you deploy your service, you will use the URI of the live service. You don't need to update the proxy.+

The following code adds an event handler that prints the request URIs to the console window. This step isn't required, but it's interesting to see the URIs for each query.+

C#Copy
container.SendingRequest2 += (s, e) =>
{
Console.WriteLine("{0} {1}", e.RequestMessage.Method, e.RequestMessage.Url);
};

Query the Service

The following code gets the list of products from the OData service.+

C#Copy
class Program
{
static void DisplayProduct(ProductService.Product product)
{
Console.WriteLine("{0} {1} {2}", product.Name, product.Price, product.Category);
} // Get an entire entity set.
static void ListAllProducts(ProductService.Container container)
{
foreach (var p in container.Products)
{
DisplayProduct(p);
}
} static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:18285/odata/");
var container = new ProductService.Container(uri);
container.SendingRequest2 += (s, e) =>
{
Console.WriteLine("{0} {1}", e.RequestMessage.Method, e.RequestMessage.Url);
}; // Get the list of products
ListAllProducts(container);
}
}

Notice that you don't need to write any code to send the HTTP request or parse the response. The proxy class does this automatically when you enumerate the Container.Products collection in the foreach loop.+

When you run the application, the output should look like the following:+

consoleCopy
GET http://localhost:60868/odata/Products
Hat 15.00 Apparel
Scarf 12.00 Apparel
Socks 5.00 Apparel
Yo-yo 4.95 Toys
Puzzle 8.00 Toys

To get an entity by ID, use a where clause.+

C#Copy
// Get a single entity.
static void ListProductById(ProductService.Container container, int id)
{
var product = container.Products.Where(p => p.ID == id).SingleOrDefault();
if (product != null)
{
DisplayProduct(product);
}
}

For the rest of this topic, I won't show the entire Main function, just the code needed to call the service.+

Apply Query Options

OData defines query options that can be used to filter, sort, page data, and so forth. In the service proxy, you can apply these options by using various LINQ expressions.+

In this section, I'll show brief examples. For more details, see the topic LINQ Considerations (WCF Data Services) on MSDN.+

Filtering ($filter)

To filter, use a where clause. The following example filters by product category.+

C#Copy
// Use the $filter option.
static void ListProductsInCategory(ProductService.Container container, string category)
{
var products =
from p in container.Products
where p.Category == category
select p;
foreach (var p in products)
{
DisplayProduct(p);
}
}

This code corresponds to the following OData query.+

consoleCopy
GET http://localhost/odata/Products()?$filter=Category eq 'apparel'

Notice that the proxy converts the where clause into an OData $filter expression.+

Sorting ($orderby)

To sort, use an orderby clause. The following example sorts by price, from highest to lowest.+

C#Copy
// Use the $orderby option
static void ListProductsSorted(ProductService.Container container)
{
// Sort by price, highest to lowest.
var products =
from p in container.Products
orderby p.Price descending
select p; foreach (var p in products)
{
DisplayProduct(p);
}
}

Here is the corresponding OData request.+

consoleCopy
GET http://localhost/odata/Products()?$orderby=Price desc

Client-Side Paging ($skip and $top)

For large entity sets, the client might want to limit the number of results. For example, a client might show 10 entries at a time. This is called client-side paging. (There is also server-side paging, where the server limits the number of results.) To perform client-side paging, use the LINQ Skip and Take methods. The following example skips the first 40 results and takes the next 10.+

C#Copy
// Use $skip and $top options.
static void ListProductsPaged(ProductService.Container container)
{
var products =
(from p in container.Products
orderby p.Price descending
select p).Skip(40).Take(10); foreach (var p in products)
{
DisplayProduct(p);
}
}

Here is the corresponding OData request:+

consoleCopy
GET http://localhost/odata/Products()?$orderby=Price desc&$skip=40&$top=10

Select ($select) and Expand ($expand)

To include related entities, use the DataServiceQuery<t>.Expand method. For example, to include the Supplier for each Product:+

C#Copy
// Use the $expand option.
static void ListProductsAndSupplier(ProductService.Container container)
{
var products = container.Products.Expand(p => p.Supplier);
foreach (var p in products)
{
Console.WriteLine("{0}\t{1}\t{2}", p.Name, p.Price, p.Supplier.Name);
}
}

Here is the corresponding OData request:+

consoleCopy
GET http://localhost/odata/Products()?$expand=Supplier

To change the shape of the response, use the LINQ select clause. The following example gets just the name of each product, with no other properties.+

C#Copy
// Use the $select option.
static void ListProductNames(ProductService.Container container)
{ var products = from p in container.Products select new { Name = p.Name };
foreach (var p in products)
{
Console.WriteLine(p.Name);
}
}

Here is the corresponding OData request:+

consoleCopy
GET http://localhost/odata/Products()?$select=Name

A select clause can include related entities. In that case, do not call Expand; the proxy automatically includes the expansion in this case. The following example gets the name and supplier of each product.+

C#Copy
// Use $expand and $select options
static void ListProductNameSupplier(ProductService.Container container)
{
var products =
from p in container.Products
select new
{
Name = p.Name,
Supplier = p.Supplier.Name
};
foreach (var p in products)
{
Console.WriteLine("{0}\t{1}", p.Name, p.Supplier);
}
}

Here is the corresponding OData request. Notice that it includes the $expand option.+

consoleCopy
GET http://localhost/odata/Products()?$expand=Supplier&$select=Name,Supplier/Name

For more information about $select and $expand, see Using $select, $expand, and $value in Web API 2.+

Add a New Entity

To add a new entity to an entity set, call AddToEntitySet, where EntitySet is the name of the entity set. For example, AddToProducts adds a new Product to the Products entity set. When you generate the proxy, WCF Data Services automatically creates these strongly-typed AddTo methods.+

C#Copy
// Add an entity.
static void AddProduct(ProductService.Container container, ProductService.Product product)
{
container.AddToProducts(product);
var serviceResponse = container.SaveChanges();
foreach (var operationResponse in serviceResponse)
{
Console.WriteLine(operationResponse.StatusCode);
}
}

To add a link between two entities, use the AddLink and SetLink methods. The following code adds a new supplier and a new product, and then creates links between them.+

C#Copy
// Add entities with links.
static void AddProductWithSupplier(ProductService.Container container,
ProductService.Product product, ProductService.Supplier supplier)
{
container.AddToSuppliers(supplier);
container.AddToProducts(product);
container.AddLink(supplier, "Products", product);
container.SetLink(product, "Supplier", supplier);
var serviceResponse = container.SaveChanges();
foreach (var operationResponse in serviceResponse)
{
Console.WriteLine(operationResponse.StatusCode);
}
}

Use AddLink when the navigation property is a collection. In this example, we are adding a product to the Products collection on the supplier.+

Use SetLink when the navigation property is a single entity. In this example, we are setting the Supplier property on the product.+

Update / Patch

To update an entity, call the UpdateObject method.+

C#Copy
static void UpdatePrice(ProductService.Container container, int id, decimal price)
{
var product = container.Products.Where(p => p.ID == id).SingleOrDefault();
if (product != null)
{
product.Price = price;
container.UpdateObject(product);
container.SaveChanges(SaveChangesOptions.PatchOnUpdate);
}
}

The update is performed when you call SaveChanges. By default, WCF sends an HTTP MERGE request. The PatchOnUpdate option tells WCF to send an HTTP PATCH instead.+

Note

Why PATCH versus MERGE? The original HTTP 1.1 specification (RCF 2616) did not define any HTTP method with "partial update" semantics. To support partial updates, the OData specification defined the MERGE method. In 2010, RFC 5789 defined the PATCH method for partial updates. You can read some of the history in this blog post on the WCF Data Services Blog. Today, PATCH is preferred over MERGE. The OData controller created by the Web API scaffolding supports both methods.+

If you want to replace the entire entity (PUT semantics), specify the ReplaceOnUpdate option. This causes WCF to send an HTTP PUT request.+

C#Copy
container.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);

Delete an Entity

To delete an entity, call DeleteObject.+

C#Copy
static void DeleteProduct(ProductService.Container container, int id)
{
var product = container.Products.Where(p => p.ID == id).SingleOrDefault();
if (product != null)
{
container.DeleteObject(product);
container.SaveChanges();
}
}

Invoke an OData Action

In OData, actions are a way to add server-side behaviors that are not easily defined as CRUD operations on entities.+

Although the OData metadata document describes the actions, the proxy class does not create any strongly-typed methods for them. You can still invoke an OData action by using the generic Execute method. However, you will need to know the data types of the parameters and the return value.+

For example, the RateProduct action takes parameter named "Rating" of type Int32 and returns a double. The following code shows how to invoke this action.+

C#Copy
int rating = 2;
Uri actionUri = new Uri(uri, "Products(5)/RateProduct");
var averageRating = container.Execute<double>(
actionUri, "POST", true, new BodyOperationParameter("Rating", rating)).First();

For more information, seeCalling Service Operations and Actions.+

One option is to extend the Container class to provide a strongly typed method that invokes the action:+

C#Copy
namespace ProductServiceClient.ProductService
{
public partial class Container
{
public double RateProduct(int productID, int rating)
{
Uri actionUri = new Uri(this.BaseUri,
String.Format("Products({0})/RateProduct", productID)
); return this.Execute<double>(actionUri,
"POST", true, new BodyOperationParameter("Rating", rating)).First();
}
}
}

[转]Calling an OData Service From a .NET Client (C#)的更多相关文章

  1. [转]Consuming a OData Service in a Client Application (WCF Data Services)

    本文转自:https://msdn.microsoft.com/zh-tw/library/dd728282(v=vs.103).aspx WCF Data Services 5.0   其他版本   ...

  2. [转]Work With Odata in Web API: Create Your First Odata Service

    本文转自:http://www.c-sharpcorner.com/UploadFile/dacca2/work-with-odata-in-web-api-create-your-first-oda ...

  3. 使用SAP云平台的destination消费Internet上的OData service

    通过SAP云平台上的destination我们可以消费Internet上的OData service或者其他通过HTTP方式暴露出来的服务. 创建一个新的destination: 维护如下属性: 点击 ...

  4. 创建OData Service(基于ASP.NET 4.6.1, EF 6),Part I:Project initialize

    由于ASP.NET Core 1处于RC阶段,加上OData WebAPI 对ASP.NET Core 1的跟进不是很积极,基于ASP.NET Core 1的Alpha 1版本已经N月没有check ...

  5. 如何用SAP WebIDE的Fiori创建向导基于ABAP OData service快速创建UI5应用

    如果我们手上已经有可以正常工作的OData服务,无论位于ABAP on-premise系统还是public上的internet OData service,都可以用SAP WebIDE里的Fiori创 ...

  6. UI5-文档-4.26-(Optional) Remote OData Service

    到目前为止,我们已经使用了本地JSON数据,但是现在我们将访问一个真正的OData服务来可视化远程数据. 用可公开获得的Northwind OData服务显示并替换发票模型的JSONModel类型,以 ...

  7. 找出OData service出错根源的小技巧

    SAP的Fiori应用是通过OData和后台交互的.在使用Fiori应用时您可能会遇到这样的错误消息: 这个错误消息没有包含有助于partner或者客户定位问题根源的线索. 下面是如何在后台找出问题根 ...

  8. 使用Excel消费C4C的OData service

    步骤比较简单, 打开Excel的标签Data->From Other Sources->From OData Data Feed: 输入如下url: https://.c4c.saphyb ...

  9. Calling the Web Service dynamically (.NET 动态访问Web Service)

    针对.NET平台下的WebService访问,为达到不添加引用的情况下,动态调用外部服务. 主体方法: public class WebServiceHelper { //Calling the We ...

随机推荐

  1. WPF文字间距

    代码: <ItemsControl ItemsSource="{Binding Info}" FontFamily="微软雅黑" FontSize=&qu ...

  2. pageadmin CMS 如何添加自定义页面

    理论上网站上的所有页面都可以通过栏目管理来添加,那自定义页面的意义是什么呢? 网站的需求是很多样化的,比如需要制作一个对外提供数据的api,甚至制作一个搜索页面,或者制作一些数据和栏目没有对应关系的页 ...

  3. 如何建立git 远程仓库

    第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开Shell ...

  4. StringBudiler源码简单解析

    StringBudiler源码 继承关系树 底层实现 默认容量() 特别的添加方法(append) 1.继承关系树 继承自AbstractStringBuilder与StringBuffer同族 2. ...

  5. java 获取一个整数的各个位数

    两种方法~  第一种是取模运算  第二种是使用char数组进行分割开依次存到数组[推荐第二种] 获取一个四位数的各个位数 int qian =input/1000; //千位除以1000       ...

  6. PHP中运算符优先级

    运算符优先级指定了两个表达式绑定得有多“紧密”.例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是因为乘号(“*”)的优先级比加号(“+”)高.必要时可以用括号来强制改变优先级.例如: ...

  7. leetcode-77-组合

    题目描述: 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], ...

  8. sqlplus登录远程数据库与数据导出

    一.登录 1.cmd中输入sqlplus /nolog 2.链接数据库,root是用户名,root123是密码,ORCL是数据库名.conn root/root123@192.168.1.27:152 ...

  9. FlowPortal-BPM——验证控件

    自上而下依次是: 非空验证.范围验证.规则表达式验证.比较验证.自定义验证 非空验证的使用: 1.ControlToValidate - 监控的控件 2.ErrorMessage - 为空时提示信息

  10. C#-WebForm-ajax状态保持

    cookies: ashx端赋值: context.Response.Cookies["Username"].Value = ""; 后台端加载: Respon ...