Server Side Paging in WCF Data Service

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.

PPT 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

Server1.gif

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.

Server2.gif

Server3.gif

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

WcfDataService.svc.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Data.Services;
usingSystem.Data.Services.Common;
usingSystem.Linq;
usingSystem.ServiceModel.Web;
usingSystem.Web;
 
namespacePagingSample
{
publicclassWcfDataService1 : DataService<StudentDBEntities>
    {

publicstaticvoidInitializeService(DataServiceConfigurationconfig)
        {

config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

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


Now when, we run the service, we can see

Server4.gif

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

Server5.gif

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

Server6.gif

How to fetch paged data at client side?

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

Server7.gif

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

Server8.gif

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

Server9.gif

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;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data.Services.Client;
using ConsoleApplication1.ServiceReference1;
 
namespace ConsoleApplication1
{
classProgram
    {
staticvoid Main(string[] args)
        {

StudentDBEntitiesent = newStudentDBEntities(newUri("http://localhost:11518/WcfDataService1.svc/"));
DataServiceCollection<Student> students = newDataServiceCollection<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,

Server10.gif

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.
  

Up Next
    Ebook Download
    View all
    Learn
    View all