次の方法で共有


コンソール アプリケーションでの OData フィードの使用 (WCF Data Services クイック スタート)

このタスクでは、Visual Studio でコンソール アプリケーションを作成し、この新しいクライアント アプリケーションに Open Data Protocol (OData) ベースの Northwind サービス サンプルへの参照を追加します。次に、生成されたクライアント データ サービス クラスおよび WCF Data Services クライアント ライブラリを使用して、アプリケーションから OData フィードにアクセスします。

コンソール アプリケーションで Northwind OData サービス サンプルを使用するには

  1. ソリューション エクスプローラーでソリューションを右クリックし、[追加] をポイントして [新しいプロジェクト] をクリックします。

  2. [プロジェクトの種類][Windows] をクリックして、[テンプレート] ペインで [コンソール アプリケーション] をクリックします。

  3. プロジェクト名として「NorthwindConsole」を入力し、[OK] をクリックします。

  4. 新しい NorthwindConsole プロジェクトを右クリックし、[サービス参照の追加] をクリックします。[アドレス] フィールドに Northwind データ サービス サンプルの URI を次のとおり入力します。

    http://services.odata.org/Northwind/Northwind.svc/
    
  5. [名前空間] ボックスに「Northwind」と入力し、[OK] をクリックします。

    これにより、必須の WCF Data Services アセンブリへの参照が追加されます。 また、プロジェクトに新しいコード ファイルも追加されます。このコード ファイルには、データ サービス リソースにアクセスし、オブジェクトとしてデータ サービス リソースと対話するデータ クラスが含まれています。 データ クラスは、名前空間 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 データ サービスの作成