Consuming OData Feeds in a Console Application (WCF Data Services Quickstart)
In this task, you will create a console application in Visual Studio, add a reference to the sample Northwind Open Data Protocol (OData)-based service into this new application, and access the OData feed from the application by using the generated client data service classes and WCF Data Services client library.
To consume the sample Northwind OData service in a console application
In Solution Explorer, right-click the solution, click Add, and then click New Project.
In Project types, click Windows, and then select Console Application in the Templates pane.
Enter NorthwindConsole for the project name, and then click OK.
Right-click the new NorthwindConsole project, click Add Service Reference, and in the Address field enter the URI of the sample Northwind data service, as follows:
http://services.odata.org/Northwind/Northwind.svc/
In the Namespace text box, type Northwind, and then click OK.
This adds references to the required WCF Data Services assemblies. It also adds a new code file to the project, which contains the data classes that are used to access and interact with data service resources as objects. The data classes are created in the namespace NorthwindConsole.Northwind.
Open the program file for the console application, and add the following using statement (Imports in Visual Basic):
Imports System.Data.Services.Client Imports NorthwindConsole.Northwind
using System.Data.Services.Client; using Northwind;
In the program file, add the following code into the Main method:
' 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); }
This code queries the Northwind data service for the orders and related line items that belong to the customer ALFKI
In Solution Explorer, right-click the NorthwindConsole project and select Set as startup project.
Press F5 to start the application.
This builds the solution and starts the client application. Data is requested from the service and displayed in the console.
Next Steps
You have successfully created a simple client application that accesses the sample Northwind OData feed. Next, you will add an ASP.NET project to the solution. This project will host a writable version of the Northwind sample OData service that runs on your local computer: