About WCF Data Service

In this article we will focus on how to create a WCF Data Service. First we will create a WCF Data Service in a Web Application. Then we will access this service from a Windows based application by adding reference to this DataService in the Windows based application.

In this article we will focus on how to create a WCF Data Service using Visual Studio 2012.

First we will create a WCF Data Service in a Web Application. Then we will access this service from a Windows based application.

Step 1:Launch Visual Studio 2012 -> File ->New Project -> ASP.NET Web Forms Application -> Name: "WCFDataServiceDemo"
Step 2:Right Click Project -> Add ->New Item -> Select Data on right side -> Select ADO.NET Entity Data Model.
In the choose model content dialog box select "Generate from database" -> Next -> In the Choose Your Data Connection dialog box -> Click New Connection ->Select the Server Name and the Database Name "EmpDB" -> Ok -> In the choose your database objects and settings dialog box -> Expand Tables -> Select Emp table -> Finish
Step 3: Creating WCF Data Service
Right Click Project -> Add -> New Item -> On the Left Side -> Under Visual C# -> Select Web -> On the Right side scroll and select "WCF Data Service"
-> In the Name Field type: "EmpDataService.svc" -> Add.
This adds the DataService to the project and opens the EmpDataService.svc.cs file in the Visual Studio code editor window.

Now you can see the below line. Which implies that the WCF DataService inherits the DataService class.

public class EmpDataService : DataService</* TODO: put your data source class name here */ >

Step 4:
Since we are using EmpDBEntities class as the datasource we need to replace the text in <> brackets to this class as shown below:

public class EmpDataService : DataService<EmpDBEntities>

Step 5:
Next remove the commented code and add the following line to this class method: "InitializeService":
config.SetEntitySetAccessRule("*", EntitySetRights.All);
This indicates that all the tables are accessible to all users. Now Build the Web Forms Aplication and Press Ctrl +F5 this open the XML schema for the service.
Step 6:
If we want to see XML representation of the data in the Emp table, type Emps at the end of the url as follows:
Step 7:
Now we will consume the WCF DataService in a Windows Forms Application. Add a New Windows Forms Application to the solution
Right click the Solution -> Add -> New Project -> Windows -> Windows Forms Application -> Name: DataServiceClient -> OK .
Right click DataServiceClient project in the solution -> Select "Set as StartUp Project".
Step 8:
Right click the DataServiceClient project -> Add Service Reference -> click on Discover -> Under Services select "EmpDataService.svc" -> This displays the URL of the service in Address field. Click on OK.
Step 9:
Go To View menu -> Other Windows -> Data Sources -> This opens the Data Source window.
Step 10:
Now Drag and Drop the Emps DataSource to Form1. This adds three controls to the Form: A DataGridView control(empsDataGridView), a BindingSource component(empsBindingSource),a BindingNavigator component(empsBindingNavigator).
Step 11:
Now Double click on the Form(Form1) and in the Form_Load event handler add the below code.

ServiceReference1.EmpDBEntities proxy = new ServiceReference1.EmpDBEntities(new Uri("http://localhost:4053/EmpDataService.svc"));
this.empsBindingSource.DataSource = proxy.Emps;

Step 12:
In the WCFDataServiceDemo project -> Right Click "EmpDataService.svc" file and select View in Browser. This displays the XML schema of the dataservice in the browser. Now copy this url and paste it in above code in the new Uri constructor.
Step 13:
Press Ctrl +F5 to run the application. This displays the webform with the Emp details.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: