Click here to Skip to main content
15,881,781 members
Articles / Programming Languages / C#

Using Paging in WCF Data Services

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
18 Jul 2010CPOL2 min read 23.7K   6   1
In the new release of WCF Data Services, we get server side paging and this will be addressed in this post.

Introduction

One of the mechanisms which was provided in WCF Data Services from the start was client side paging. In the new release of WCF Data Services, we also get server side paging and this will be addressed in this post.

WCF Data Services Client Side Paging

From the early days of WCF Data Services, we could achieve paging on the client side using the $top and $skip query parameters. For example, the following URI for a data service will bring the 11-20 courses which were requested:

http://localhost:8322/SchoolDataService.svc/Courses?$skip=10&top=10

The problem starts when you expose resources with a lot of items. Let's say that we have 10000 courses in our course library. The following URI will bring to the client all of them:

http://localhost:8322/SchoolDataService.svc/Courses

Running this request will decrease the amount of clients for the service since it will take ages for the query to return. So what can we do? Use server side paging instead.

WCF Data Services Server Side Paging

So how can we limit our returning result set to the client? We can use a new feature of WCF Data Services. In order to create a server side paging behavior, all we need to do is to configure the service to return some portion of data. We will use the new method
of the DataServiceConfiguration class which is called SetEntitySetPageSize for each entity set or for all of them at once (using * ). The following code creates server side paging for all the entity sets exposed by the data service to return only 10 items at once:

C#
public class SchoolDataService : DataService<SchoolEntities>
{    
  public static void InitializeService(DataServiceConfiguration config)
  {
    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
    config.SetEntitySetPageSize("*", 10);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  }
}

When I use the service now with a URI to retrieve all the courses, I will get only the first ten courses. Also, as you can see in the figure, we get a link at the bottom that leads us to the next ten results:

Server Side Paging

and the link itself is written as:

http://localhost:8322/SchoolDataService.svc/Courses?$skiptoken=4041

Summary

In the new release of WCF Data Services, we get a new paging feature which enables server side paging. Using this method or combining it with client side paging extends the capabilities of WCF Data Services. The use of the server side paging is very easy – just configure the service. In the post, I showed an example of how you can achieve that behavior.

Image 2

Image 3
Image 4

Image 5 Image 6 Image 7 Image 8 Image 9 Image 10 Image 11 Image 12 Image 13 Image 14

Image 15

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
-- No messages could be retrieved (timeout) --