Visual Studio 2008 SP1: Querying an ADO.Net Data Service via .Net Client

12 במאי 2008

no comments

Visual Studio 2008 SP1: Querying an ADO.Net Data Service via .Net Client

This post is a part of a series of posts Exploring Beta 1 of Visual Studio 2008 and .Net Framework 3.5, and specifically, the ADO.Net Data Services framework that will ship as part of it.

In part1 I had a step by step guide for building a simple web data service, that exposed Entity Sets of the Entity Data Model I've created. Then, I used the browser to request data from the Data Service. Although it was very nice, it was not useful in real applications. In this post, I will talk about how we can query the Data Service from a .net client application. This post is a step by step guide for creating a client application that queries the Blog Data Service I build in the previous post.

1. Create a .Net Client Application

Create a new Console Application, and add a reference the the client side components for ADO.Net Data Services assembly (System.Data.Services.Client.dll, formerly called Microsoft.Data.WebClient.dll).


2. Generating Client Side Entities with datasvcutil.exe

Open the Blog Data Service from the previous post and browse to the service (http://localhost:1039/BlogDataService.svc), just to make sure we can access the service add pull its metadata. If you want to explore the data service metadata you can add /$metadata to the url (http://localhost:1039/BlogDataService.svc/$metadata)

Open a command windows and navigate to C:\WINDOWS\Microsoft.NET\Framework\v3.5 folder. In order to generate the client classes, we use the datasvcutil.exe tool with the following command:

datasvcutil.exe /uri:http://localhost:1039/BlogDataService.svc /out:BlogEntities.cs

This command queries the metadata of the Data Service from the input uri and generates the entities code in a file called BlogEntities.cs.

Microsoft (R) DataSvcUtil version
Copyright (C) 2008 Microsoft Corporation. All rights reserved.

Writing object layer file…

Generation Complete — 0 errors, 0 warnings


Add the BlogEntities.cs code file to the client project. This adds the generated classes to the project and makes them available for use in the client application.



3. Querying using DataServiceContext and DataServiceQuery<T>

There are several ways in which we can query our data service:

  1. .

  2. Using the generated class that inherits from DataServiceContext.

  3. Using LINQ to ADO.Net Data Services.

I. The untyped approach using DataServiceContext

Create an instance of DataServiceContext, and pass the url of the data service as a parameter.

DataServiceContext svc = new DataServiceContext(new Uri("http://localhost:1039/BlogDataService.svc"));

Now, in order to query for the list of posts, you can create a query.

var query = ctx.CreateQuery<Post>("Posts");

foreach (Post p in query)


  Console.WriteLine("{0} posted on {1}", p.Title, p.PublishDate);



The DataServiceContext.CreateQuery<T> takes the relative path of the Entity Set we want to query, and returns an instance of DataServiceQuery<T> which is IQueryable. This means that only when we first enumerate the results, the query will be translated into a web request and be sent to the Data Service. If we hover the query object while debugging, we can see the URI of the request that will be sent to the service in order to perform the query.


II. Using the generated class that inherits from DataServiceContext

One of the classes that datasvcutil.exe has generated is BlogEntities, which is very similar to the approach taken by the LINQ to SQL DataContext and Entity Framework's ObjectContext.


public partial class BlogEntities : System.Data.Services.Client.DataServiceContext



    public BlogEntities(global::System.Uri serviceRoot) : base(serviceRoot) { }

    public System.Data.Services.Client.DataServiceQuery<Blogs> Blogs


        get { return base.CreateQuery<Blogs>("Blogs"); }


    public System.Data.Services.Client.DataServiceQuery<Categories> Categories


        get { return base.CreateQuery<Categories>("Categories"); }


    public System.Data.Services.Client.DataServiceQuery<Posts> Posts


        get { return base.CreateQuery<Posts>("Posts"); }




It is a partial class that inherits from the "untyped" DataServiceContext and exposes a strongly typed DataServiceQuery<T> objects for the data service entity sets as properties. The constructors takes the base URL of the service as a "connection string".

In order to use the typed Blogentities, we can write:

BlogEntities svc = new BlogEntities(new Uri("http://localhost:1039/BlogDataService.svc"));

var query = svc.Posts;

foreach (Posts p in query)


    Console.WriteLine("{0} posted on {1}", p.Title, p.PublishDate);



We can even take a more sophisticated query. Since the DataServiceQuery<T> is an IQueryable, you can use it's extension methods in order to add operators to the query such as:

var query = svc.Posts.Where(p1=>p1.PostID == 2);

which results in the following URI for querying the data service: http://localhost:1039/BlogDataService.svc/Posts(2)/

var query = svc.Posts.OrderBy(p=>p.PublishDate).Skip(2).Take(2);

which produces: http://localhost:1039/BlogDataService.svc/Posts()?$orderby=PublishDate&$skip=2&$top=2

and you get the idea…

III. LINQ to ADO.Net Data Services

8. Use LINQ to ADO.Net Data Services to query the service. Again, since the DataServiceQuery is an IQueryable, you can use LINQ syntax to query it:

var query = (from p in svc.Posts

            where p.PostID > 3

            orderby p.PublishDate

            select p).Skip(2).Take(2);


ADO.Net Data Services provides a rich mechanism for querying a data service from a .net client application. We start by creating the DataServiceContext, and using DataServiceQuery objects to perform queries. The datasvcutil.exe tool can generate the client side entities for us.


Add comment
facebook linkedin twitter email