建立 .NET Framework 用戶端應用程式 (WCF 資料服務快速入門)
這是 WCF Data Services 快速入門的最後一項工作。 在此工作中,您會在方案中加入主控台應用程式、在這個新的用戶端應用程式中加入 Open Data Protocol (OData) 摘要的參考,並從用戶端應用程式中利用已產生的用戶端資料服務類別和用戶端程式庫來存取 OData 摘要。
![]() |
---|
不需要 .NET Framework 架構的用戶端應用程式也可存取資料摘要。 此資料服務可由取用 OData 摘要的任何應用程式元件所存取。 如需詳細資訊,請參閱使用用戶端應用程式中的資料服務 (WCF 資料服務)。 |
若要使用 Visual Studio 建立用戶端應用程式
在 [方案總管] 中,以滑鼠右鍵按一下方案,然後依序按一下 [加入] 和 [新增專案]。
在 [專案類型] 中,按一下 [視窗],然後在 [範本] 窗格中選取 [WPF 應用程式]。
針對專案名稱輸入 NorthwindClient,然後按一下 [確定]。
開啟 MainWindow.xaml 檔案,並以下列程式碼取代 XAML 程式碼:
<Window x:Class="MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" Title="Northwind Orders" Height="335" Width="425" Name="OrdersWindow" Loaded="Window1_Loaded"> <Grid Name="orderItemsGrid"> <ComboBox DisplayMemberPath="OrderID" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="true" Height="23" Margin="92,12,198,0" Name="comboBoxOrder" VerticalAlignment="Top"/> <DataGrid ItemsSource="{Binding Path=Order_Details}" CanUserAddRows="False" CanUserDeleteRows="False" Name="orderItemsDataGrid" Margin="34,46,34,50" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Product" Binding="{Binding ProductID, Mode=OneWay}" /> <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity, Mode=TwoWay}" /> <DataGridTextColumn Header="Price" Binding="{Binding UnitPrice, Mode=TwoWay}" /> <DataGridTextColumn Header="Discount" Binding="{Binding Discount, Mode=TwoWay}" /> </DataGrid.Columns> </DataGrid> <Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top" HorizontalAlignment="Left" Width="65">Order:</Label> <StackPanel Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right" Height="40" Margin="0,257,22,0"> <Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="buttonSave" VerticalAlignment="Bottom" Width="75" Click="buttonSaveChanges_Click">Save Changes </Button> <Button Height="23" Margin="0,0,12,12" Name="buttonClose" VerticalAlignment="Bottom" Width="75" Click="buttonClose_Click">Close</Button> </StackPanel> </Grid> </Window>
若要將資料服務參考加入至專案
以滑鼠右鍵按一下 NorthwindClient 專案,依序按一下 [加入服務參考] 和 [探索]。
這會顯示您在第一個工作中建立的 Northwind 資料服務。
在 [命名空間] 文字方塊中,輸入 Northwind,然後按一下 [確定]。
這會將新的程式碼檔案加入到專案中,此專案包含的資料類別可用來存做為物件的資料服務資源,並與之進行互動。 在命名空間
NorthwindClient.Northwind
中建立資料類別。
在 WPF 應用程式中存取資料服務的資料
在 [NorthwindClient] 下的 [方案總管] 中,以滑鼠右鍵按一下專案,然後按一下 [加入參考]。
在 [加入參考] 對話方塊中,按一下 [.NET ] 索引標籤,然後選取 System.Data.Services.Client.dll 組件,再按一下 [確定]。 在 [NorthwindClient] 下的 [方案總管] 中,開啟 MainWindow.xaml 檔案的字碼頁,並加入下列 using 陳述式 (在 Visual Basic 中為 Imports)。
Imports System.Data.Services.Client Imports NorthwindClient.Northwind
using System.Data.Services.Client; using NorthwindClient.Northwind;
插入下列查詢資料服務的程式碼,並將 DataServiceCollection 的結果繫結到 MainWindow 類別裡:
注意:
您必須用裝載 Northwind 資料服務之執行個體的伺服器及連接埠來取代主機名稱 localhost:12345
。Private context As NorthwindEntities Private customerId As String = "ALFKI" ' Replace the host server and port number with the values ' for the test server hosting your Northwind data service instance. Private svcUri As Uri = New Uri("https://localhost:12345/Northwind.svc") Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Try ' Instantiate the DataServiceContext. context = New NorthwindEntities(svcUri) ' Define a LINQ query that returns Orders and ' Order_Details for a specific customer. Dim ordersQuery = From o In context.Orders.Expand("Order_Details") _ Where o.Customer.CustomerID = customerId _ Select o ' Create an DataServiceCollection(Of T) based on ' execution of the LINQ query for Orders. Dim customerOrders As DataServiceCollection(Of Order) = New _ DataServiceCollection(Of Order)(ordersQuery) ' Make the DataServiceCollection<T> the binding source for the Grid. Me.orderItemsGrid.DataContext = customerOrders Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End Sub
private NorthwindEntities context; private string customerId = "ALFKI"; // Replace the host server and port number with the values // for the test server hosting your Northwind data service instance. private Uri svcUri = new Uri("https://localhost:12345/Northwind.svc"); private void Window1_Loaded(object sender, RoutedEventArgs e) { try { // Instantiate the DataServiceContext. context = new NorthwindEntities(svcUri); // Define a LINQ query that returns Orders and // Order_Details for a specific customer. var ordersQuery = from o in context.Orders.Expand("Order_Details") where o.Customer.CustomerID == customerId select o; // Create an DataServiceCollection<T> based on // execution of the LINQ query for Orders. DataServiceCollection<Order> customerOrders = new DataServiceCollection<Order>(ordersQuery); // Make the DataServiceCollection<T> the binding source for the Grid. this.orderItemsGrid.DataContext = customerOrders; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
將下列儲存變更的程式碼插入 MainWindow 類別中:
Private Sub buttonSaveChanges_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Try ' Save changes made to objects tracked by the context. context.SaveChanges() Catch ex As DataServiceRequestException MessageBox.Show(ex.ToString()) End Try End Sub Private Sub buttonClose_Click(ByVal sender As Object, ByVal a As RoutedEventArgs) Me.Close() End Sub
private void buttonSaveChanges_Click(object sender, RoutedEventArgs e) { try { // Save changes made to objects tracked by the context. context.SaveChanges(); } catch (DataServiceRequestException ex) { MessageBox.Show(ex.ToString()); } } private void buttonClose_Click(object sender, RoutedEventArgs e) { this.Close(); }
建置和執行 NorthwindClient 應用程式
在 [方案總管] 中,以滑鼠右鍵按一下 NorthwindClient 專案,然後選取 [設定為啟始專案]。
請按 F5 啟動應用程式。
如此會建置方案,並啟動用戶端應用程式。 從服務要求資料,並顯示在主控台中。
編輯資料格的 [數量] 欄,然後按一下 [儲存]。
資料服務的變更會被儲存。
注意:
此版本的 NorthwindClient 應用程式不支援新增及刪除實體。
後續步驟
您已成功建立存取範例 Northwind OData 摘要的用戶端應用程式。 您也已經完成 WCF Data Services 快速入門。如需詳細資訊從 .NET Framework 應用程式存取 OData 摘要的詳細資訊,請參閱WCF Data Services 用戶端程式庫。