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

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

No Comments