How To: Generate Data Access Layer Based On Data Provided By a WCF Service
- 4 minutes to read
This topic gives a step-by-step description of how to create a WCF Data Service and a client application to work with a remote database through the web service.
Note
Requirements The tutorial is written for Visual Studio 2013. If you are using other versions of Visual Studio, the steps might differ.
This lesson consists of the following sections:
- Create an ASP.NET Web Application
- Generate Entity Framework Data Models and Create a WCF Data Service
- Bind a WPF Application to a WCF Data Service
- What is Next?
Create an ASP.NET Web Application
Open Visual Studio and create a blank solution by selecting FILE | New | Project in the main menu and choose the Blank Solution item.
Add a new ASP.NET Web project. Right click the solution in the Solution Explorer and choose New Project… | Add.
Find the ASP.NET Empty Web Application item in the opened window and click OK.
Generate Entity Framework Data Models and Create a WCF Data Service
First, you will create an Entity Data Model that represents all tables in the Northwind database. The steps are the same ones described in the How To: Generate Data Access Layer with Entity Framework Database First topic. Follow the steps described in the linked topic to generate the Entity Framework model in the created ASP.NET Web Application, and return to follow the other steps in this topic.
When the an Entity Data Model is generated, you can create a WCF Data Service that provides access to the database.
To use WCF Data Services with Entity Framework 6 or higher, you need to install WCF Data Services EntityFramework Provider. Right-click the project in Solution Explorer and select the Manage NuGet Packages option.
Find and install the WCF Data Services EntityFrameworkProvider package.
Now, you can create a WCF Data Service that allows you to get access to a database. To add a WCF Data Service to the NorthwindService application, right-click the project in the Solution Explorer and select the Add | New Item option. In the Add New Item dialog box, input the WCF text to the search box, and then choose the WCF Data Service item.
In the Name text box, enter NorthwindWcfDataService and click the Add button.
The NorthwindWcfDataService.svc file is shown in the Code Editor.
public class NorthwindWcfDataService : DataService< /* TODO: put your data source class name here */ > {
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config) {
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
// config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
Since you chose Entity Framework version 6.0 when creating an entity data model, the data service should inherit from the EntityFrameworkDataService<NorthwindEntities> class.
public class NorthwindWcfDataService : EntityFrameworkDataService<NorthwindEntities> {
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config) {
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
You can test the created service. Run the project to open a browser window with the XML service schema.
Bind a WPF Application to a WCF Data Service
Add a new WPF application to the solution (you can use the Template Gallery to do this) and set it as start up project.
Next, you will add a service reference to the NorthwindWcfDataService. Right-click on the WPF project and select Add | Add Service Reference.
In the Add Service Reference dialog box, click the Discover button. The URL for the NorthwindWcfDataService service appears in the Address field. Click the OK button to add the service reference.
What is Next?
When the Data Access Layer is generated, you can use our Scaffolding Wizard to generate the other parts of a database-oriented application. The UI Generation topic describes how to do this step-by-step.