Introduction to ADO.Net Data Services (Part 1 - Creating a Data Service)
ADO.Net Data Services (codename: Astoria) is a feature that allows users to find, manipulate, and deliver data through the use of simple URIs. This allows easy access to data over the web and allows separation of business and data logic. The concept comes from a general programming concept referred to as REST.
As of right now, ADO.Net Data Services is available in an “Extension” to .net 3.5. Eventually it will be part of the .net 3.5 sp1 framework. However, for now, you have to do some extra steps to install it (i.e.: install the asp.net extension preview).
This series of blogs is intended to do a walkthrough of creating ADO.Net Data Services and consuming them in client applications. I have written these blogs as reference material to Code Camp speeches I am doing throughout the D.C/NoVa/Md area. These blogs are not intended to describe the benefits or the why’s of ADO.Net Data Services, they are just intended to show how to work with a simple example.
I will list below the environment setup, that is needed right now, to get ADO.Net Data Services working on your computer.
-
Supported Operating Systems - Windows Server 2003, Windows Vista, Windows XP
-
.Net Framework 3.5
-
Visual Studio 2008 or Visual Web Developer 2008 Express Edition
-
ASP.Net Extensions Preview -http://www.microsoft.com/downloads/details.aspx?FamilyId=A9C6BC06-B894-4B11-8300-35BD2F8FC908&displaylang=en (PLEASE NOTE: as of today 3/21/2008 the Extensions are the way to use this feature, but soon Microsoft will role this into .net 3.5 sp1. So, if you are reading this blog after .net 3.5 sp1 comes out, you might not have to do this step)
-
OPTIONAL - SQL Server 2005 and the AdventureWork Database. This is just needed for the example I will be writing about in this blog. This is not needed for ADO.Net Data Services. The backend database is irrelevant to work with Data Service (as long as .net can work with it). The AdventureWork Database is just Microsofts example database and it is easy for me to use to show people how to work with ADO.Net DataServices in this blog.
After your computer is setup correctly you can start building the ADO.Net Data Service. Below I will write up a simple example of leveraging the ADO.Net Data Service, as well as the Entity Framework to display a database in a RESTful manner over a web page.
-
Open up Visual Stuido 2008.
-
Click File/New/Project…
-
Choose a Visual C#/ASP.NET Web Application (use C# for this example, it can be VB or any other language, but I am writing this blog assuming c#)
-
Delete the Default.aspx that was automatically created.
-
Right click on the WebApplication project and click Add/New Item…
-
Choose an ADO.NET Entity Data Model and click Add
-
The Entity Data Model is another new feature of .net that builds out an entire data access/object model for you.
-
It is very powerful and we are going to take advantage of some of its features for this demo, but I recommend you looking into it more as it is a great new feature of .net.
-
NOTE: I am using the Entity Data Model for this example because of how easy it is. However, you can create your own custom objects, with your own custom data access if you want. You just have to make sure the objects implement IQueryable.
-
-
A Wizard will come up
-
Make sure Generate from database is selected and click Next
-
Create a Connection to your database (if one doesn’t already exist) throug the New Connection… button.
-
Make sure your Data source is SQL Server
-
Choose your Server name
-
Choose your Authentication to the database
-
Select the AdventureWorks database (note: this will only be there if you have installed it, if it is not there Microsoft makes this database available over the internet).
-
-
Click Next
-
Uncheck Views and Stored Procedures - thus leaving Tables as the only checked entity to use, click Finish.
-
Congratulations! You just created an entire object model and data access layer, based off your database. It is that easy to work with the Entity Framework.
-
-
Right click on the WebApplication project and click Add/New Item…
-
Choose the ADO.NET Data Service.
-
Lastly, we need to expose the object we created before (with the Entity Framework) to the web in this new Data Service. To do this we need to do two things. Both of these things are represented in the code block below.
-
Tell the service which object to use. This is the object we put in the WebDataService<….> inheritence of the class.
-
Tell the service which parts of the object should be visible. We are going to make * visible (which means everything). This is the line we put in the Intialize Service.
public class WebDataService1 : WebDataService< AdventureWorksModel.AdventureWorksEntities > { // This method is called once during service initialization to allow // service-specific policies to be set public static void InitializeService(IWebDataServiceConfiguration config) { config.SetResourceContainerAccessRule("*", ResourceContainerRights.All); } // Query interceptors, change interceptors and service operations go here } -
- We have just successfully exposed our data through a DataService. Now you can right click on your svc file and set it as the Start Page. Then you can debug your service (F5) to see what you have created.
- A WebPage should come up and you should see xml similar to below:
NOTE: If you don’t see something similar to what is above there are 3 reasons:
-
The extensions and all the prerequisites are not installed correctly on your computer
-
The instructions were either not followed correctly, or I messed up somewhere (it is possible, but I have ran through them a bunch).
-
You have RSS feeds turned on in your internet browser. If this is the case the webpage will look more like an RSS feed than an XML blob. To turn RSS feeds off go to your tools/options of your browser. Find the Feeds/Settings (this will be different for every browser). Turn off the Automatically marking of feeds when reading feeds (this will be stated differently for every browser).
This is part one of this blog. There will be 2 more parts:
Part 2: Reading and Manipulating ADO.Net Data Services results in the browser
Part 3: Reading and Manipulating ADO.Net Data Services results in a .Net consumer application

