How to create first Windows 7 mobile Application Video

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

clip_image002

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.

clip_image004

clip_image006

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

clip_image008

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

clip_image010

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

clip_image012

How to fetch paged data at client side?

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

clip_image014

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

clip_image016

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

clip_image018

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,

clip_image020

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.

Creating WCF Data Service from scratch and Hosting in Console Application

Objective

In this article, I will show how

1. WCF Service can be created from the scratch

2. WCF Service can be hosted in Console Application

You can see video of this article here

Let us say, we have a table called Student of below structure below in our data base

clip_image001

In Student table RollNumber is a primary key.

Let us create WCF Data Service which will expose the above table as REST service

Step 1

Create a new project. Select the project template Console Application from Windows tab.

clip_image003

Step 2: Create a Data Model

We can create a Data Model, which can be exposed as WCF Data Service in three ways

1. Using ADO.Net Entity model.

2. Using LINQ to SQL class.

3. Custom Data Model.

For our purpose, I am going to use ADO.Net Entity model to create the data model. So to create an entity model

1. Right click on Console application and add a new item

2. Select ADO.Net Entity model from Data tab.

clip_image005

3. Since we have table in data base. So we are going to choose option, select from database.

clip_image006

4. Either choose the data base from drop down or create a new data connection.

clip_image007

In above connection string StudentDBEntities is name of the connection string. If we want, we can change this connection string as per our requirement.

If your required data base is not listed in drop down then, you can create a new data connection. To create new data connection click on New Connection

clip_image008

You can give the data base server name and press refresh. After pressing Refresh, you can choose the data base from the drop down. After selecting the data base click on Test Connection to test connection established successfully or not?

5. Select tables, views and stored procedure from data base you want to make as the part of your data model. Since we are having only one table so we are selecting one table.

clip_image009

If you want you can change name of the data model. By default it is name of the data base appended by the term model. Click on Finish button to complete and create the data model.

6. Now we can see that StudentDataModel.edmx has been created in the designer.

clip_image010

Since there is only one table, so there is only one table model at design surface.

Now we have created the data model which can be exposed as WCF Data Service. Now you can see in solution explorer, you have StudentModel.edmx and StdentModel.Designer.cs files.

clip_image011

Step 3

Add reference of

System.ServiceModel;

System.ServiceModel.Web;

System.Data.Services;

System.Data.Services.Client

Step 4

Right click and add a class in console application. I am giving name here MyDataService to the class.

clip_image013

Step 5

Add the below namespaces in the class MyDataService

clip_image014

Step 6

In this step we will create WCF Data Service class.

clip_image016

1. Make the class as public.

2. Inherit DataService class.

3. Create a public method InitializeService

4. Pass the input parameter of the type DataServiceConfiguration

5. Set the protocol version to DataServiceProtocol version 2.0

MyDataService.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Data.Services;

using System.Data.Services.Common;

namespace SelfHosted

{

public class MyDataService :DataService<StudentDBEntities>

{

public static void InitializeService(DataServiceConfiguration config)

{

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

config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

}

}

}


Step 7

Host the WCF Data Service in Console application. So to do that , write the below code in Program.cs

clip_image018

1. Create instance of WebServiceHost.

2. Create instance of WebHttpBinding.

3. Add service end point with Contract IRequestHandler

4. Open the host

Full source code for hosting program is as below ,

Programs.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Data.Services;

using System.Data.Services.Common;

namespace SelfHosted

{

class Program

{

static void Main(string[] args)

{

WebServiceHost host = new WebServiceHost(typeof(MyDataService),

new Uri("http://localhost:9999/DataService"));

WebHttpBinding binding = new WebHttpBinding();

host.AddServiceEndpoint(typeof(IRequestHandler),

binding, "WebServiceHost");

host.Open();

Console.WriteLine("Service at http://localhost:9999/DataService ");

Console.WriteLine("Press any key to stop the Service ");

Console.Read();

host.Close();

}

}

}


Step 8

Just press F5 to run the WCF Data Service. Data Service will be hosted in the console application.

On running you can see, one table is listed. That table is Student

clip_image020

So, we are able to run the WCF Data Service and this service Is hosted in console application. We can consume the service in any client in the same way; we consume WCF Data service hosted in any other web server.

I hope this post was useful, thanks for reading. Happy coding.