Skip to main content
A newer version of this page is available. .

How To: Generate Data Access Layer Based On Data Provided By a WCF Service

  • 4 minutes to read

In this example you will create a Data Access Layer that works with the WCF Service, which retrieves data from a remote database.

  1. In Visual Studio, go to “File | New | Project” or press CTRL+SHIFT+N to create a new project. Select the “Blank Solution” template to create a solution with no projects.

    Scaffolding - WCF - Add Solution

  2. Right click the solution in Solution Explorer window, then click “Add | New Project…”. Find the “ASP.NET Empty Web Application” template and click OK. You will need this project to emulate the server that exposes data for your application.

    Scaffolding - WCF - Add ASP Project

  3. You now require an Entity Framework Data Model that represents data tables and views from the sample “Northwind” database. To add this Data Model, follow steps described in the How To: Generate Data Access Layer with Entity Framework Database First tutorial, starting from step two.
  4. After your Data Model is ready, you can proceed to adding a WCF Data Service that will establish a connection to the database and provide access to its data. Before you do so, go to Visual Studio’s “Project | Manage NuGet Packages…” menu and install the WCF Data Services EntityFramework Provider package. This one allows you to use WCF Data Services with Entity Framework 6 and higher.

    Scaffolding - WCF - Install Package

  5. In the Solution Explorer window, select your ASP.NET project and press “CTRL+SHIFT+A” to add a new item. Search for the “WCF Data Service” item, then click “Add”.

    Scaffolding - WCF - Add WCF

  6. Open the .svc file added to your project and inspect its code. Notice that Visual Studio suggests you add the data source class name manually.

    
    public class WcfDataService1 : 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 your Data Model was generated with Entity Framework 6, the data service should inherit the EntityFrameworkDataService<Northwind2010Entities> class.

    
    public class WcfDataService1 : EntityFrameworkDataService<Northwind2010Entities>
    {
        // 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;
        }
    }
    
  7. You now have the server side ready, so add a blank WinForms project to your solution. Once the project is added, right-click it and select the “Set as StartUp Project” option.
  8. Right-click your project in the Solution Explorer window and choose the “Add Service Reference” menu item.

    Scaffolding - WCF - Add Service Reference

  9. In the “Add Service Reference” dialog that pops up, click the “Discover” button to browse for existing WCF Data Services. Make sure your service URL appears in the “Address” field, then click OK to finish.

    Scaffolding - WCF - Add Service Reference Dialog

What’s Next?

Congratulations, you have created a sample Data Access Layer with data provided by a WCF Service! Check out other Data Access Layer tutorials or proceed to generating an application based on this layer.