Using Microsoft ADO.Net Data Services with Virtuoso

Introduction:

The goal of Microsoft® ADO.NET Data Services is to enable applications to expose data as a data service that can be consumed by web clients within corporate networks and across the internet. A data service is reachable via regular HTTP requests, using standard HTTP verbs such as GET, POST, PUT and DELETE to perform CRUD operations against the service. The payload format used by the service is controllable by the application, but all options are simple, open formats such as JSON and Atom/APP.

The use of web-friendly technologies make ADO.NET Data Services ideal as a data back-end for AJAX-style applications, Rich Interactive Applications and other applications that need to operate against data that is stored across the web.

Getting Started: Creating Data Services

Pre-requisites

In order to create a data service using ADO.NET Data Services in your own environment you will need:

1. Microsoft Visual Studio 2008 SP1, the ADO.NET Entity Framework runtime and associated tools are included in Visual Studio 2008 SP1.

2. A running Virtuoso Universal Server instance.

Selecting a Data Source

The ADO.NET Data Service server framework is comprised of two halves. The top-half is the runtime itself; this part is “fixed”, and it implements URI translation, the Atom/JSON wire formats, the interaction protocol, etc. This is what makes an ADO.NET Data Service look like an ADO.NET Data Service. The bottom half is the data-access layer and is pluggable. Communication between layers happens in terms of the IQueryable interface plus a set of conventions to map CLR graphs into the URI/payload patterns of ADO.NET Data Services.

The first step in creating an ADO.NET Data Service is to determine the data source that is to be exposed as a set of REST-based endpoints (ie. select or create a data access layer). For relational data stored in Microsoft SQL Server or other 3rd Party databases, ADO.NET Data Services currently enables easily exposing a conceptual model created using the ADO.NET Entity Framework (EF). For all other data sources (XML document, web service, application logic layer, etc) or to use additional database access technologies (ex. LINQ to SQL), a mechanism is provided which enables any data source, as per the plug-in model described above, to be exposed as an ADO.NET Data Service.

To create a data service which exposes a relational database through an Entity Framework conceptual model see “Creating a Data Service using the ADO.NET Entity Framework”. To create a data service which exposes another data source see “Creating a Data Service from any Data Source”.

Creating a Data Service using the ADO.NET Entity Framework

ADO.NET Data Services are a specialized form of Windows Communication Foundation services, and thus can be hosted in various environments. The below example will create an ADO.NET Data Service which is hosted inside an ASP.NET site. In order to create a data service, you must first create a web project; you will then need to establish a connection with the database that will be exposed by the service, and then create the data service itself within the web application. Below is a step-by-step description of this process.

The following steps can be used for creating a Data Service using the Virtuoso ADO.Net Provider for accessing the sample Northwind Demo database:

1. Launch the Visual Studio 2008 SP1 IDE.




2. Create a Web Application project by going to the File menu in Visual Studio and choosing New Project.

3. When the New Project window appears, choose either Visual Basic or Visual C# as the programming language.

4. Within the language category click on Web, and select ASP.NET Web Application from the right-hand panel.

5. Choose a name for the project, for example VirtuosoDataService, and click OK.




6. This will create a new project called VirtuosoDataService.




7. Right click on the VirtuosoDataService project name of the Solution Explorer pane, then select the Add -> New Item menu options.




8. The Add New Item dialog will appear, choose the ADO.NET Entity Data Model template, give it the name Virtuoso.edmx and click Add to start the creation of the ADO.Net Entity Data Model.




9. In the Entity Data Model Wizard dialog Choose Model Contents page select the Generate from Database model type and click Next.




10. In the Entity Data Model Wizard dialog Choose your Data Connection page select the New Connection button.




11. In the Choose Data Source dialog, select the OpenLink Virtuoso Data Source from the list displayed and click Continue.




12. In the Connection Properties dialog specify the hostname, portno, username and password for the target Virtuoso Server and click the Advanced button.




13. In the Advanced Properties dialog set the Database paramter to Demo and click OK.




14. Press the Test Connection dialog to verify the database is accessible.




15. Set the entity connect string name to VirtuosoDemoEntities (note this name as it is required in step 17 below) and click Next.




16. In the Choose your Database Objects page select the Tables check box to select all tables in the Demo database for addition to the Enity Data Model, set the Model Namespace to VirtuosoDemoModel and click Finish.




17. The Virtuoso.edmx EDM will be created with the tables and relationships displayed in the Visual Studio IDE




18. Right click on the VirtuosoDataService project name of the Solution Explorer pane, then select the Add -> New Item menu options.




19. The Add New Item dialog will appear, choose the ADO.NET Data Service template, give it the name Virtuoso.svc and click Add to create the ADO.Net Data Service.




20. In the Virtuoso.svc.cs Data Service file created add the data source class name of VirtuosoDemoEntities (note this is the name set in step 12) as the DataService name and enable the access to the Data Service by adding the entry config.SetEntitySetAccessRule("**", EntitySetRights.All); in the InitializeService method.

// C#

using System;
using System.Web;
using System.Collections.Generic;
using System.ServiceModel.Web;
using System.Linq;
using System.Data.Services;

namespace SimpleDataService
{
    public class Northwind : DataService<VirtuosoDemoEntities>
    {
        public static void InitializeService(IDataServiceConfiguration  config)
        {
            config.SetEntitySetAccessRule("**", EntitySetRights.All);
        }
    }
}

21. To test the Data Service, simply hit Ctrl+F5 within Visual Studio, which will start the development web server, run the Data Services server inside and load a Web browser page displaying the list of available tables/entities of the Demo database.




22. To access a specific entity instance like the Customers table ALFKI record, this would be specified as http://host/vdir/Virtuoso.svc/Customers('ALFKI') .




NOTES

1. Important - To view Atom (the default format returned by an ADO.NET Data Service) in Internet Explorer, you must first ensure that Feed Reading View is turned off . This can be done on the Content tab of Tools in Internet Options.

2. If a Data Services entity instance URI page fails to load you can turn Verbose errors on by adding config.UseVerboseErrors

true; in the virtuoso.svc.cs InitializeService method to obtain more detailed information from the server as to why the page failed to load:

public static void InitializeService(IDataServiceConfiguration config)

{

config.UseVerboseErrors = true;

config.SetEntitySetAccessRule("**", EntitySetRights.All);

}

Referenced by...