在控制台应用程序中使用 OData 源(WCF Data Services 快速入门)

在此任务中,将在 Visual Studio 中创建一个控制台应用程序,并将对基于示例 Northwind 开放式数据协议 (OData) 服务的引用添加到此新建的应用程序中,然后使用生成的客户端数据服务类和 WCF 数据服务 客户端库从此应用程序访问 OData 源。

在控制台应用程序中使用示例 Northwind OData 服务

  1. 在**“解决方案资源管理器”中,右击解决方案,单击“添加”,然后单击“新建项目”**。

  2. 在**“项目类型”中,单击“Windows”,然后在“模板”窗格中选择“控制台应用程序”**。

  3. 输入 NorthwindConsole 作为项目名称,然后单击**“确定”**。

  4. 右击新建的 NorthwindConsole 项目,单击**“添加服务引用”,在“地址”**字段中输入示例 Northwind 数据服务的 URI,如下所示:

    http://services.odata.org/Northwind/Northwind.svc/
    
  5. 在**“命名空间”文本框中,键入 Northwind,然后单击“确定”**。

    这会将引用添加到所需的 WCF 数据服务 程序集。 这还将在项目中添加一个新的代码文件,其中包含用于作为对象访问数据服务资源并与其交互的数据类。 这些数据类是在命名空间 NorthwindConsole.Northwind 中创建的。

  6. 打开控制台应用程序的程序文件,并添加以下 using 语句(在 Visual Basic 中为 Imports):

    Imports System.Data.Services.Client
    Imports NorthwindConsole.Northwind
    
    using System.Data.Services.Client;
    using Northwind;
    
  7. 在程序文件中,将以下代码添加到 Main 方法:

    ' Define the URI of the public Northwind OData service.
    Dim northwindUri As Uri = _
        New Uri("http://services.odata.org/Northwind/Northwind.svc/", _
            UriKind.Absolute)
    
    ' Define a customer for filtering.
    Const customer As String = "ALFKI"
    
    ' Create a new instance of the typed DataServiceContext.
    Dim context As NorthwindEntities = _
        New NorthwindEntities(northwindUri)
    
    ' Create a LINQ query to get the orders, including line items, 
    ' for the selected customer.
    Dim query = From order In context.Orders.Expand("Order_Details") _
                Where order.CustomerID = customer _
                Select order
    Try            
        Console.WriteLine("Writing order ID and line item information...")
    
        ' Enumerating returned orders sends the query request to the service.
        For Each o As Order In query
    
            Console.WriteLine("Order ID: {0}", o.OrderID)
    
            For Each item As Order_Detail In o.Order_Details
    
                Console.WriteLine(vbTab & "Product ID: {0} -- Quantity: {1}", _
                    item.ProductID, item.Quantity)
            Next                
        Next
    Catch ex As DataServiceQueryException            
        Console.WriteLine(ex.Message)
    End Try
    
    // Define the URI of the public Northwind OData service.
    Uri northwindUri =
        new Uri("http://services.odata.org/Northwind/Northwind.svc/",
            UriKind.Absolute);
    
    // Define a customer for filtering.
    const string customer = "ALFKI";
    
    // Create a new instance of the typed DataServiceContext.
    NorthwindEntities context = new NorthwindEntities(northwindUri);
    
    // Create a LINQ query to get the orders, including line items, 
    // for the selected customer.
    var query = from order in context.Orders.Expand("Order_Details")
                where order.CustomerID == customer
                select order;
    try
    {
        Console.WriteLine("Writing order ID and line item information...");
    
        // Enumerating returned orders sends the query request to the service.
        foreach (Order o in query)
        {
            Console.WriteLine("Order ID: {0}", o.OrderID);
    
            foreach (Order_Detail item in o.Order_Details)
            {
                Console.WriteLine("\tProduct ID: {0} -- Quantity: {1}",
                    item.ProductID, item.Quantity);
            }
        }
    }
    catch (DataServiceQueryException ex)
    {
        Console.WriteLine(ex.Message);
    }
    

    此代码将向 Northwind 数据服务查询属于客户 ALFKI 的订单和相关行项目

  8. 在**“解决方案资源管理器”中,右击“NorthwindConsole”项目,然后选择“设为启动项目”**。

  9. 按 F5 键启动该应用程序。

    这将生成解决方案并启动客户端应用程序。 将从服务请求数据并将其显示在控制台中。

后续步骤

您已成功创建了用于访问 Northwind OData 示例源的简单客户端应用程序。 接下来,要将 ASP.NET 项目添加到解决方案中。 此项目将承载在本地计算机上运行的 Northwind 示例 OData 服务的可写版本。

创建 Northwind 数据服务