Server side paging in WCF Data Service

No.of Views1248
Bookmarked0 times
Downloads 
Votes1
By  Dhananjay Kumar   On  03 Jul 2010 07:07:51
Tag : WCF , Utilities
In this article, I will show you, how we can achieve server side paging in WCF Data service
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, I will show you, how we can achieve server side paging in WCF Data service?
Note:   IF you are new to WCF Data Service, please read my other articles on WCF Data Service.

Introduction to WCF Data Service

Here, I am assuming that, you have basic understanding of WCF Data service. So I will start with  the code in .svc file  or service file.
WcfDataService.svc.cs 

Image Loading

If we see in above code, we are setting the access rule for all the entity in the model to allow only the read operation.

How to enable paging at server side?

To enable paging at server side, we need to set the page the entity page size and also we need to explicitly set the version of the protocol to V2. 

Image Loading
Image Loading

We are telling here that return only one record for all the entities .

WcfDataService.svc.cs

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace PagingSample
{public class WcfDataService1 : DataService<StudentDBEntities >
    {public static void InitializeService(DataServiceConfiguration config)
        {
            
             config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

             config.SetEntitySetPageSize("*", 1);           
             config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }
}

 Now when, we run the service, we can see 

Image Loading

When, we navigate to Students table, we get the above result. 

Image Loading

If , we notice above result , we can see , a link that will be used to navigate to next records. 

Image Loading

How to fetch paged data at client side?

If we fetch the data at the client side in normal way as below, 

Image Loading

Output, you will get name of only first student, because page size at server side is set to 1 . 

Image Loading

So, if we want to get all the record through paging, we need to use Continuation property of DataServiceCollection 

Image Loading

So, above code will load the DataServiceCollection with all the data from the service.  And then we can use normal foreach statement to print all the records

Programs.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1;

namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {
           
            StudentDBEntities ent = new StudentDBEntities(new Uri("http://localhost:11518/WcfDataService1.svc/"));
            DataServiceCollection<Student> students = new DataServiceCollection<Student>(ent.Students);while (students.Continuation != null)
            {
                students.Load(ent.Execute<Student>(students.Continuation));
            }foreach (var r in students)
            {
                Console.WriteLine(r.Name);
            }
            Console.Read();
        }
    }
}

 When you run output would be as below,

Image Loading

So, this was all about how to enable server side paging in WCF Data Service. Thanks for reading. I hope article was useful.  Happy Coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top