ADO.Net Data Services Part 3 – Querying a Web Data Service and WebDataGen.exe

20 בדצמבר 2007

ADO.Net Data Services Part 3 – Querying a Web Data Service and WebDataGen.exe

This post is part of a series of posts about ADO.Net Data Services, a part of the ASP.Net 3.5 Extensions. In ADO.Net Data Services Part 1 – Building a Simple Web Data Service I had a step by step guide for building a simple web data service, that exposed resources as IQueryable properties of the DataContext. In ADO.Net Data Services Part 2 – Using Service Operations with WebGet I added some Service Operations to the Data Service, and worked with them. In both of the previous posts I used the browser to request data from the Data Service which was very nice, but not useful in real applications. In this post, I will talk about how we can query the Data Service from a 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 posts.

1. Open the Blog Data Service from ADO.Net Data Services Part 2 – Using Service Operations with WebGet post.

2. Add a new Console Application to the solution, and add a reference the the client side components for ADO.Net Data Services assembly (Microsoft.Data.WebClient.dll that is one of the assemblies that came with the ASP.Net 3.5 Extensions Preview, and can be found in C:\Program Files\Reference Assemblies\Microsoft\Framework\ASP.NET 3.5 Extensions).

WebDataGen.exe LINQ to ADO.Net Data Services WebDataContext WebDataQuery

Using the WebDataGen.exe tool

3. Generate the client side entities. Open a command windows and navigate to C:\Program Files\Microsoft ASP.NET 3.5 Extensions folder. In order to generate the client classes, we use the WebDataGen.exe tool with the following command:

Webdatagen.exe /mode:ClientClassGeneration
/uri:http://localhost:2445/BlogData.svc /outobjectlayer: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) WebDataGen version
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Writing object layer file…

Generation Complete — 0 errors, 0 warnings

4. 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.

LINQ to ADO.Net Data Services WebDataContext WebDataQuery

WebDataContext and WebDataQuery

5. Query using an untyped WebDataContext. Create an instance of WebDataContext, and pass the url of the data service as a parameter.

WebDataContext ctx = new WebDataContext("http://localhost:2445/BlogData.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 WebDataContext.CreateQuery<T> takes the relative path of the resource we want to query, and returns an instance of WebDataQuery<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.

LINQ to ADO.Net Data Services WebDataContext WebDataQuery

6. Query using the generated WebDataContext. One of the classes that WebDataGen.exe has generated is BlogDataContext, which is very similar to the approach taken by the LINQ to SQL DataContext and Entity Framework's ObjectContext.

public partial class BlogDataContext : Microsoft.Data.WebClient.WebDataContext


    public BlogDataContext(string uriString) : base(uriString) { }

    public BlogDataContext(System.Uri baseUri) : base(baseUri) { }


    public Microsoft.Data.WebClient.WebDataQuery<Blog> Blogs


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



    public Microsoft.Data.WebClient.WebDataQuery<Post> Posts


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



    public Microsoft.Data.WebClient.WebDataQuery<Category> Categories


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



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

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

BlogDataContext ctx = new BlogDataContext("http://localhost:2445/BlogData.svc");


var query = ctx.Posts;


foreach (Post p in query) { … }

7. Use a more sophisticated query. Since the WebDataQuery is an IQueryable, you can use it's extension methods in order to add operators to the query such as:

var query = ctx.Posts.Where(p=>p.BlogID == "bursteg");

which results in the following URI for querying the data service: http://localhost:2445/BlogData.svc/Posts?$filter=(BlogID)eq('bursteg')

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

which produces: http://localhost:2445/BlogData.svc/Posts?$orderby=PublishDate&$skip=2&$top=2

and you get the idea…

LINQ to ADO.Net Data Services

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

var query = (from p in ctx.Posts

            where p.BlogID == "bursteg"

            orderby p.PublishDate

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


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


Add comment
facebook linkedin twitter email


  1. Ufuk Çoban30 בדצמבר 2007 ב 21:48

    is this service read only? can i insert or update data via this service?

  2. skmbxnogqwd12 בינואר 2008 ב 13:02

    96jVne moripmwrrblc, [url=]dxrjxytqxbsz[/url], [link=]jgpoikkxbirp[/link],

  3. Luke6 בפברואר 2008 ב 4:30

    Great Post.
    The finer details (like namespace references and folder locations) you have included saved me hours of time – much appreciated.

  4. Cheap Car Rental Reservations5 בינואר 2009 ב 0:55

    Do you think that car rentals will continue to get cheaper?

  5. Liposuction7 בינואר 2009 ב 6:50

    I have to disagree with that last comment…doesn't make sense

  6. Personal Injury Lawyers27 בינואר 2009 ב 6:33

    Who are you picking in the super bowl?

  7. AftennuAgue12 ביוני 2009 ב 2:38

    visit us!

  8. enabuttemalay13 ביוני 2009 ב 7:20

Comments are closed.