<docbook><section><title>VirtUsingMsAdoNetDataServicesWithVirtuoso</title><bridgehead class="http://www.w3.org/1999/xhtml:h2">Using Microsoft ADO.Net Data Services with Virtuoso</bridgehead>
<bridgehead class="http://www.w3.org/1999/xhtml:h3">Introduction:</bridgehead>
<para>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.</para><para>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.</para><bridgehead class="http://www.w3.org/1999/xhtml:h3">Getting Started: Creating Data Services</bridgehead>
<bridgehead class="http://www.w3.org/1999/xhtml:h4">Pre-requisites</bridgehead>
<para>In order to create a data service using ADO.NET Data Services  in your own environment you will need:</para><para>1.
 Microsoft Visual Studio 2008 SP1, the ADO.NET Entity Framework runtime and associated tools are included in Visual Studio 2008 SP1.</para><para>2.
 A running Virtuoso Universal Server instance.</para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Selecting a Data Source</bridgehead>
<para>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.</para><para>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.</para><para>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”.</para><bridgehead class="http://www.w3.org/1999/xhtml:h3">Creating a Data Service using the ADO.NET Entity Framework</bridgehead>
<para>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.</para><para>The following steps can be used for creating a Data Service using the Virtuoso ADO.Net Provider for accessing the sample Northwind Demo database:</para><para>1.
 Launch the Visual Studio 2008 SP1 IDE.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_1.png" /></figure></para><para>2.
 Create a <emphasis>Web Application</emphasis> project by going to the <emphasis>File</emphasis> menu in Visual Studio and choosing <emphasis>New Project</emphasis>.
 </para><para>3.
 When the New Project window appears, choose either <emphasis>Visual Basic</emphasis> or <emphasis>Visual C#</emphasis> as the programming language.</para><para>4.
 Within the language category click on <emphasis>Web</emphasis>, and select <emphasis>ASP.NET Web Application</emphasis> from the right-hand panel.
 </para><para>5.
 Choose a name for the project, for example <emphasis>VirtuosoDataService</emphasis>, and click <emphasis>OK</emphasis>.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_2.png" /></figure></para><para>6.
 This will create a new project called <emphasis>VirtuosoDataService</emphasis>.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_3.png" /></figure></para><para>7.
 Right click on the <emphasis>VirtuosoDataService</emphasis> project name of the <emphasis>Solution Explorer</emphasis> pane, then select the <emphasis>Add</emphasis> -&gt; <emphasis>New Item</emphasis> menu options.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_4.png" /></figure></para><para>8.
 The <emphasis>Add New Item</emphasis> dialog will appear, choose the <emphasis>ADO.NET Entity Data Model</emphasis> template, give it the name <emphasis>Virtuoso.edmx</emphasis> and click <emphasis>Add</emphasis> to start the creation of the ADO.Net Entity Data Model.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_5.png" /></figure></para><para>9.
 In the <emphasis>Entity Data Model Wizard</emphasis> dialog <emphasis>Choose Model Contents</emphasis> page select the <emphasis>Generate from Database</emphasis> model type and click <emphasis>Next</emphasis>.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_6.png" /></figure></para><para>10.
 In the <emphasis>Entity Data Model Wizard</emphasis> dialog <emphasis>Choose your Data Connection</emphasis> page select the <emphasis>New Connection</emphasis> button.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_7.png" /></figure></para><para>11.
 In the <emphasis>Choose Data Source</emphasis> dialog, select the <emphasis>OpenLink Virtuoso Data Source</emphasis> from the list displayed and click <emphasis>Continue</emphasis>.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_8.png" /></figure></para><para>12.
 In the <emphasis>Connection Properties</emphasis> dialog specify the <emphasis>hostname</emphasis>, <emphasis>portno</emphasis>, <emphasis>username</emphasis> and password for the target Virtuoso Server and click the <emphasis>Advanced</emphasis> button.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_9.png" /></figure></para><para>13.
 In the <emphasis>Advanced Properties</emphasis> dialog set the <emphasis>Database</emphasis> paramter to <emphasis>Demo</emphasis> and click <emphasis>OK</emphasis>.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_10.png" /></figure></para><para>14.
 Press the <emphasis>Test Connection</emphasis> dialog to verify the database is accessible.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_11.png" /></figure></para><para>15.
 Set the <emphasis>entity connect string</emphasis> name to <emphasis>VirtuosoDemoEntities</emphasis> (note this name as it is required in step 17 below) and click <emphasis>Next</emphasis>.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_12.png" /></figure></para><para>16.
 In the <emphasis>Choose your Database Objects</emphasis> page select the <emphasis>Tables</emphasis> check box to select all tables in the Demo database for addition to the Enity Data Model, set the <emphasis>Model Namespace</emphasis> to <emphasis>VirtuosoDemoModel</emphasis> and click <emphasis>Finish</emphasis>.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_13.png" /></figure></para><para>17.
 The <emphasis>Virtuoso.edmx</emphasis> EDM will be created with the tables and relationships displayed in the Visual Studio IDE <figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_14.png" /></figure></para><para>18.
 Right click on the <emphasis>VirtuosoDataService</emphasis> project name of the <emphasis>Solution Explorer</emphasis> pane, then select the <emphasis>Add</emphasis> -&gt; <emphasis>New Item</emphasis> menu options.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_4.png" /></figure></para><para>19.
 The <emphasis>Add New Item</emphasis> dialog will appear, choose the <emphasis>ADO.NET Data Service</emphasis> template, give it the name <emphasis>Virtuoso.svc</emphasis> and click <emphasis>Add</emphasis> to create the ADO.Net Data Service.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_15.png" /></figure></para><para>20.
 In the <emphasis>Virtuoso.svc.cs</emphasis> Data Service file created add the data source class name of <emphasis>VirtuosoDemoEntities</emphasis> (note this is the name set in step 12) as the <emphasis>DataService</emphasis> name and enable the access to the Data Service by adding the entry config.SetEntitySetAccessRule(&quot;<computeroutput>**</computeroutput>&quot;, EntitySetRights.All); in the <emphasis>InitializeService</emphasis> method.</para><programlisting>// 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&lt;VirtuosoDemoEntities&gt;
    {
        public static void InitializeService(IDataServiceConfiguration  config)
        {
            config.SetEntitySetAccessRule(&quot;**&quot;, EntitySetRights.All);
        }
    }
}

</programlisting><figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_16.png" /></figure>
<para>21.
 To test the Data Service, simply hit <emphasis>Ctrl+F5</emphasis> 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.
<figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_17.png" /></figure></para><para>22.
 To access a specific entity instance like the <emphasis>Customers</emphasis> table <emphasis>ALFKI</emphasis> record, this would be specified as <emphasis><ulink url="http://host/vdir/Virtuoso.svc/Customers('ALFKI')">http://host/vdir/Virtuoso.svc/Customers(&#39;ALFKI&#39;)</ulink> </emphasis> . <figure><graphic fileref="VirtUsingMsAdoNetDataServicesWithVirtuoso/VirtAdoNetDataServices_18.png" /></figure></para><bridgehead class="http://www.w3.org/1999/xhtml:h3">NOTES</bridgehead>
<para>1.
 <emphasis>Important</emphasis> - To view <emphasis>Atom</emphasis> (the default format returned by an ADO.NET Data Service) in Internet Explorer, you must first ensure that <emphasis>Feed Reading View</emphasis> is turned <emphasis>off</emphasis> . This can be done on the <emphasis>Content tab</emphasis> of <emphasis>Tools<emphasis> in <emphasis>Internet Options</emphasis>.</emphasis></emphasis></para><para>2.
 If a Data Services entity instance URI page fails to load you can turn <emphasis>Verbose</emphasis> errors on by adding <emphasis>config.UseVerboseErrors </emphasis></para><para> true; in the <emphasis>virtuoso.svc.cs InitializeService </emphasis> method to obtain more detailed information from the server as to why the page failed to load: </para><programlisting>public static void InitializeService(IDataServiceConfiguration config)

{

config.UseVerboseErrors = true;

config.SetEntitySetAccessRule(&quot;**&quot;, EntitySetRights.All);

}
</programlisting></section></docbook>