在主控台應用程式中取用 OData 摘要 (WCF Data Services 快速入門)
在此工作中,您會在 Visual Studio 中建立主控台應用程式、在這個新的應用程式中加入範例 Northwind Open Data Protocol (OData) 架構服務的參考,並使用已產生的用戶端資料服務類別和 WCF Data Services 用戶端程式庫來存取應用程式中的 OData 摘要。
若要在主控台應用程式中取用範例 Northwind OData 服務
在 [方案總管] 中,以滑鼠右鍵按一下方案,然後依序按一下 [加入] 和 [新增專案]。
在 [專案類型] 中,按一下 [視窗],然後在 [範本] 窗格中選取 [主控台應用程式]。
針對專案名稱輸入 NorthwindConsole,然後按一下 [確定]。
以滑鼠右鍵按一下新的 NorthwindConsole 專案,再按一下 [加入服務參考],然後在 [位址] 欄位中輸入範例 Northwind 資料服務的 URI,如下所示:
http://services.odata.org/Northwind/Northwind.svc/
在 [命名空間] 文字方塊中,輸入 Northwind,然後按一下 [確定]。
這樣會加入必要 WCF Data Services 組件的參考。 這也會將新的程式碼檔案加入至專案中,此專案包含的資料類別可用來存取做為物件的資料服務資源,並與之進行互動。 在命名空間 NorthwindConsole.Northwind 中建立資料類別。
開啟主控台應用程式的程式檔案,並加入下列 using 陳述式 (在 Visual Basic 中則為 Imports):
Imports System.Data.Services.Client Imports NorthwindConsole.Northwind
using System.Data.Services.Client; using Northwind;
在程式檔案中,將下列程式碼貼入 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 客戶的訂單和相關產品項目。
在 [方案總管] 中,以滑鼠右鍵按一下 NorthwindConsole 專案,然後選取 [設定為啟始專案]。
請按 F5 啟動應用程式。
如此會建置方案,並啟動用戶端應用程式。 從服務要求資料,並顯示在主控台中。
後續步驟
您已成功建立簡單的用戶端應用程式來存取範例 Northwind OData 摘要。 接下來,您會將 ASP.NET 專案加入至方案中。 這個專案將會裝載可寫入的 Northwind 範例 OData 服務版本,此版本會在您的本機電腦上執行: