ADO.Net Data Services Part 1 – Building a Simple Web Data Service

12 בדצמבר 2007

ADO.Net Data Services Part 1 – Building a Simple Web Data Service

This post is part of my Get Started with ADO.Net Data Services post series. This post is a step by step guide for building a Simple Web Data Service for the Blog database I posted in the last post.

1. Create a new standard ASP.Net Web Application. Notice that there is no special project template for a Web Data Service, and later we will see the new item template. In this guide, I called my web application BlogWebApp.

image 

2. Create the application Data Model. One of the improvements of ADO.Net Data Services December CTP is that the data model doesn't have to be an Entity Data Model (ADO.Net Entity Framework Model), but it can be any class that has properties that implement the IQueryable interface. The LINQ to SQL Data Context is a great candidate for begin a data mode for an ADO.Net Data Service, since it has public properties of Table<T> that implement this interface.

Add a new item of LINQ to SQL classes to the application. In this guide, I called my model Blog.dbml.

image 

In the Server Explorer, add a new connection to the blog database.

image

After the connection has been established, expand the connection's tree node, and drag the tables to the LINQ to SQL Designer.

Linq to SQL Designer

3. Create the Web Data Service. Add a new ADO.Net Data Service item to the project. This is the new item template that is installed as part of the CTP of ADO.Net Data Services. in this guide I called this service BlogData.svc.

image

Creating this data service adds a new item to the project, and opens the service for editing.

4. Edit the Web Data Service. First, replace the template comment in the class definition

/* TODO: put your data source class name here */

with the name of the data provider. In this guide, since we are using LINQ to SQL, we will use the BlogDataContext class as a provider. This class is one of the classes that Visual Studio has generated when we created the LINQ to SQL model.

public class BlogData : WebDataService< BlogDataContext >

{

    public static void InitializeService(IWebDataServiceConfiguration config)

    {

    }

}

5. Run the project, and enable debugging if the dialog appears. The ASP.Net Web Server starts and the Internet Explorer with it, navigating to the data service: http://localhost:2445/BlogData.svc/. Notice that the output returns no results, and the output looks like:

image

The reason is that as of the December CTP of ADO.Net Data Service, Access Control was integrated into the Data Services, not allowing anyone to view the metadata or the data itself of any resource, without explicitly allowing it. I will not dive into this access control features in this post, and will dedicate a post talking about it.

6. Allow all the resources to be readable. Edit the Web Data Service, and in the InitializeService method, use the configuration parameter to do this.

public static void InitializeService(IWebDataServiceConfiguration config)

{

  config.SetResourceContainerAccessRule("*", ResourceContainerRights.AllRead);

}

This call gives all the resources (the public IQueryable properties of the DataContext) the ability to be seen via the service.

7. If you now run the service (http://localhost:2445/BlogData.svc/) again, you can see the following output:

image

If you cannot see the output in the above way, go to Tools->Internet Options-> Content Tab. Click the Settings button in the Feeds section, an uncheck the Turn on Feed Reading View option.

Notice that some elements contain the href attribute with an additional URL. We can use it to navigate through the data:

image

image

  • Show this blog's 3rd post.

image

And so on…

Enjoy!

Add comment
facebook linkedin twitter email

7 comments

  1. Robb Schiefer12 בדצמבר 2007 ב 17:50

    I followed your post and it worked but when I tried it with my own db the collections didn't show up. Did you have to do anything special to your db design to make it work?

    Also what is the deal with the display of the xml? I viewed your db in Firefox, IE7, Opera and Safari but none showed the collection pages (Posts for instance) correctly. I assumed it was a CSS issue. The page source did show the correct data.

  2. Guy Burstein [MVP]13 בדצמבר 2007 ב 9:51

    Hi Robb,

    I didn't do anything special to make the DB work with ADO.Net Data Services. Just a guess – Did you know if all your tables have primary keys?

    About the Xml layout – I'm using IE7 and had to change the feed view settings as I wrote in the post.

    Hope this helps,
    Guy

  3. Diane Sapriel30 בדצמבר 2007 ב 10:14

    I would like to build a ADO Net Data Services Web

  4. Diane Sapriel30 בדצמבר 2007 ב 10:18

    Thanks for the demonstration. I am getting feedback

  5. Luke6 בפברואר 2008 ב 4:38

    Hi Robb – I am sure you have probably figured this out already BUT – you have to add a [DataWebKey] attribute to the PK of each table with the LINQ to SQL model to allow each entity to be uniquely queried (alternatively you can make sure that each entity in your model has an "ID" or "(EntityName)ID" field as their primary key.

  6. ulqzvcj2 בדצמבר 2008 ב 18:05

    szrKAN bpmnrkqtwnzb, [url=http://yxugjnirongd.com/]yxugjnirongd[/url], [link=http://mjcpkqdpsobq.com/]mjcpkqdpsobq[/link], http://wkbhiaurezmd.com/

Comments are closed.