OData Programming Cookbook for .NET Developers
上QQ阅读APP看书,第一时间看更新

Building an OData service via WCF Data Service and ADO.NET Entity Framework

There are various means to create an OData service on the .NET Framework platform. And by using different means, we might need to choose different kind of data sources to provide the actual data that will be published and exposed in the OData service. In this recipe, we will start from one of the most typical approaches—creating an OData service through WCF Data Service and ADO.NET Entity Framework data model.

Getting ready

As we will use ADO.NET Entity Framework as the data source of our OData service, make sure you have a sample database, such as Northwind, installed in a local SQL Server instance. You can use SQL Express instance (the free version of SQL Server) for convenience.

The source code for this recipe can be found in the \ch01\ODataEFServiceSln\ directory.

How to do it...

To concentrate on the OData service generation and make the progress simple and clear, we will use an empty ASP.NET web application with a single OData service for demonstration. The detailed steps are as follows:

  1. Launch Visual Studio 2010 IDE.
  2. Fire the New Project menu and create an ASP.NET Empty Web Application through the Add New Project wizard (see the following screenshot).
    How to do it...
  3. Use the Project | Add New Item context menu to add a new ADO.NET Entity Data Model (see the following screenshot).
    How to do it...

    The wizard will guide you on selecting a source database (such as the Northwind database used in this case) .The following screenshot shows the entity classes generated through the Northwind sample database:

    How to do it...
  4. Create a new OData service via the WCF Data Service item template.

    The WCF Data Service item template can be found in the Visual Studio 2010 built-in template list (see the following screenshot).

    How to do it...

    By clicking on the Add button, Visual Studio will automatically generate the .svc file and its associated code files for the WCF Data Service item.

  5. Use View Code context menu to open the source file of the generated WCF Data Service and replace the default service type (the generic parameter) with the Entity Framework model class (generated in the previous step).

    The following code snippet shows the WCF Data Service, which uses the Northwind data model class in this sample:

    namespace ODataEFService
    {
    public class NWDataService : DataService< ODataEFService. NorthwindEntities >
    {
    public static void InitializeService(DataServiceConfiguration config)
    {
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    config.SetEntitySetAccessRule ("*", EntitySetRights.All);
    }
    }
    }
    

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  6. Now, we can start running the service by selecting the .svc file in Solution Explorer and choose the View in browser context menu.

    The default page of the WCF Data service will display all the OData entities that have been exposed in the service (see the following screenshot).

    How to do it...

How it works...

In our sample web project, there are only two items. One is the ADO.NET Entity Framework data model and the other is the WCF Data Service item (as shown in the following project structure screenshot).

How it works...

WCF Data Service has helped encapsulate all the underlying details of implementing an OData service. When using WCF Data Service to generate OData service, what we need to do is:

  • Prepare the data source provider type (in our case, the ADO.NET Entity Framework model)
  • Associate the data source provider with the WCF Data Service

Also, as the name indicates, WCF Data Service is a special implementation of WCF service. And more specifically, WCF Data Service is a specially implemented WCF service over the REST HTTP endpoint (by using the WebHttpBinding binding type). In most cases, we do not need to take care of those WCF service-specific configuration details (in web.config file). If we open the web.config file of our sample service, we can find that there is almost nothing defined within the<system.serviceModel> element for the WCF configuration (see the following screenshot).

How it works...

See also

  • Exploring an OData service through web browser recipe in Chapter 2, Working with OData at Client Side
  • Applying basic access rules on WCF Data Service recipe in Chapter 3, OData Service Hosting and Configuration