DCSIMG
ADO.Net Data Services Part 1 - Building a Simple Web Data Service - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

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

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!

Comments

Robb Schiefer said:

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.

# December 12, 2007 5:50 PM

גיא בורשטיין said:

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

# December 13, 2007 9:51 AM

Guy Burstein [MVP] said:

ADO.Net Data Services Part 2 - Using Service Operations with WebGet This post is part of a series of

# December 16, 2007 10:03 AM

Mike Taulty's Blog said:

Something that's been foxing me is that I've read about ADO.NET Data Services now supporting a "provider"...

# December 19, 2007 8:05 PM

Guy Burstein [MVP] said:

ADO.Net Data Services Part 3 - Querying a Web Data Service and WebDataGen.exe This post is part of a

# December 20, 2007 8:53 AM

Guy Burstein [MVP] said:

ADO.Net Data Services Feedback: DataContextServiceProvider Required for LINQ to SQL support After writing

# December 20, 2007 2:16 PM

Diane Sapriel said:

I would like to build a ADO Net Data Services Web

# December 30, 2007 10:14 AM

Diane Sapriel said:

Thanks for the demonstration. I am getting feedback

# December 30, 2007 10:18 AM

Diane Sapriel said:

No comment

# December 30, 2007 10:18 AM

Luke said:

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.

# February 6, 2008 4:38 AM

OnlyLiu said:

在ASP.NETAJAX中使用Web服务

ADO.NetDataServicesPart1-BuildingaSimpleWebDataService

WCF通信...

# March 22, 2008 11:51 AM

Johan's Avanade Blog said:

I have been working lately with ADO.NET Data Services, and I found several tutorials on how to create...

# October 16, 2008 7:49 PM

ulqzvcj said:

szrKAN  <a href="bpmnrkqtwnzb.com/.../a>, [url=http://yxugjnirongd.com/]yxugjnirongd[/url], [link=http://mjcpkqdpsobq.com/]mjcpkqdpsobq[/link], http://wkbhiaurezmd.com/

# December 2, 2008 6:05 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: