Walkthrough: Creating and Accessing a WCF Data Service in Visual Studio
This walkthrough demonstrates how to create a simple WCF Data Service that is hosted in an ASP.NET Web application and then access it from a Windows Forms application.
In this walkthrough you will:
Create a Web application to host a WCF Data Service.
Create an Entity Data Model that represents the Customers table in the Northwind database.
Create a WCF Data Service.
Create a client application and add a reference to the WCF Data Service.
Enable data binding to the service and generate the user interface.
Optionally add filtering capabilities to the application.
To create a WCF Data Service, you will add a Web project, create an Entity Data Model, and then create the service from the model.
In the first step, you will add a Web project to host the service.
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.
To create the Web project
On the menu bar, choose File, New, Project.
In the New Project dialog box, expand the Visual Basic or Visual C# and Web nodes, and then choose the ASP.NET Web Application template.
In the Name text box, enter NorthwindWeb, and then choose the OK button.
In the New ASP.NET Project dialog box, in the Select a template list, choose Empty, and then choose the OK button.
In this step, you will create an Entity Data Model that represents the Customers table in the Northwind database.
To create the Entity Data Model
On the menu bar, choose Project, Add New Item.
In the Add New Item dialog box, choose the Data node, and then choose the ADO.NET Entity Data Model item.
In the Name text box, enter NorthwindModel, and then choose the Add button.
The Entity Data Model Wizard appears.
In the Entity Data Model Wizard, on the Choose Model Contents page, choose the EF Designer from database item, and then choose the Next button.
On the Choose Your Data Connection page, perform one of the following steps:
If a data connection to the Northwind sample database is available in the drop-down list, choose it.
Choose the New Connection button to configure a new data connection. For more information, see How to: Create Connections to SQL Server Databases.
If the database requires a password, choose the Yes, include sensitive data in the connection string option button, and then choose the Next button.
If a dialog box appears, choose Yes to save the file to your project.
On the Choose your version page, choose the Entity Framework 5.0 option button, and then choose the Next button.
In order to use the latest version of the Entity Framework 6 with WCF Services, you’ll need to install the WCF Data Services Entity Framework Provider NuGet package. See Using WCF Data Services 5.6.0 with Entity Framework 6+.
On the Choose Your Database Objects page, expand the Tables node, select the Customers check box, and then choose the Finish button.
The entity model diagram will be displayed, and a NorthwindModel.edmx file will be added to your project.
In this step, you will create and test the data service.
To create the data service
On the menu bar, choose Project, Add New Item.
In the Add New Item dialog box, choose the Web node, and then choose the WCF Data Service 5.6 item.
In the Name text box, enter NorthwindCustomers, and then choose the Add button.
The NorthwindCustomers.svc file appears in the Code Editor.
In the Code Editor, locate the first TODO: comment and replace the code with the following:
Replace the comments in the InitializeService event handler with the following code:
On the menu bar, choose Debug, Start Without Debugging to run the service. A browser window opens and the XML schema for the service is displayed.
In the Address bar, enter Customers at the end of the URL for NorthwindCustomers.svc, and then choose the ENTER key.
An XML representation of the data in the Customers table is displayed.
In some cases, Internet Explorer will misinterpret the data as an RSS feed. You must make sure that the option to display RSS feeds is disabled. For more information, see Troubleshooting Service References.
Close the browser window.
In the next steps, you will create a Windows Forms client application to consume the service.
To create the client application, you will add a second project, add a service reference to the project, configure a data source, and create a user interface to display the data from the service.
In the first step, you will add a Windows Forms project to the solution and set it as the startup project.
To create the client application
On the menu bar, choose File, Add, New Project.
In the New Project dialog box, expand the Visual Basic or Visual C# node and choose the Windows node, and then choose Windows Forms Application.
In the Name text box, enter NorthwindClient, and then choose the OK button.
In Solution Explorer, choose the NorthwindClient project node.
On the menu bar, choose Project, Set as StartUp Project.
In this step, you will add a service reference to the WCF Data Service in the Web project.
To add a service reference
On the menu bar, choose Project, Add Service Reference.
In the Add Service Reference dialog box, choose the Discover button.
The URL for the NorthwindCustomers service appears in the Address field.
Choose the OK button to add the service reference.
In this step, you will configure a data source to enable data binding to the service.
To enable data binding to the service
On the menu bar, choose View, Other Windows, Data Sources.
In the Data Sources window, choose the Add New Data Source button.
On the Choose a Data Source Type page of the Data Source Configuration Wizard, choose Object, and then choose the Next button.
On the Select the Data Objects page, expand the NorthwindClient node, and then expand the NorthwindClient.ServiceReference1 node.
Select Customer check box, and then choose the Finish button.
In this step, you will create the user interface that will display the data from the service.
To create the user interface
In the Data Sources window, open the shortcut menu for the Customers node and choose Copy.
In the Form1.vb or Form1.cs form designer, open the shortcut menu and choose Paste.
Choose the CustomersDataGridView control, and then in the Properties window set the Dock property to Fill.
In Solution Explorer, open the shortcut menu for the Form1 node and choose View Code to open the Code Editor, and add the following Imports or Using statement at the top of the file:
Add the following code to the Form1_Load event handler:
In Solution Explorer, open the shortcut menu for the NorthwindCustomers.svc file and choose View in Browser. Internet Explorer opens and the XML schema for the service is displayed.
Copy the URL from the Internet Explorer address bar.
In the code that you added in step 4, select http://localhost:53161/NorthwindCustomers.svc/ and replace it with the URL that you just copied.
On the menu bar, choose Debug, Start Debugging to run the application. The customer information is displayed.
You now have a working application that displays a list of customers from the NorthwindCustomers service. If you want to expose additional data through the service, you can modify the Entity Data Model to include additional tables from the Northwind database.
In the next optional step, you will learn how to filter the data that is returned by the service.
In this step, you will customize the application to filter the data by the customer's city.
To add filtering by city
In Solution Explorer, open the shortcut menu for the Form1.vb or Form1.cs node and choose Open.
Open the shortcut menu for the Button control, and choose View Code, and then add the following code in the Button1_Click event handler:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim proxy As New northwindEntities _ (New Uri("http://localhost:53161/NorthwindCustomers.svc")) Dim city As String = TextBox1.Text If city <> "" Then Me.CustomersBindingSource.DataSource = From c In _ proxy.Customers Where c.City = city End If End Sub
In the previous code, replace http://localhost:53161/NorthwindCustomers.svc with the URL from the Form1_Load event handler.
On the menu bar, choose Debug, Start Debugging to run the application.
In the text box, enter London, and then choose the button. Only the customers from London are displayed.