How to work with Custom Method in WCF Data Service?

In this post we will see how could we expose a custom method in WCF Data Service? There are three steps involved in doing that

  1. Create a partial class of Entity class and add desired custom method in that.
  2. Add a method with WebGet attribute in service class. This method will make a call to the custom method added in partial entity class.
  3. At the client side call custom method using Execute method

Add Method in Partial Entity class

Create a partial class for entity class. If you are working on School database and your entity class name is SchoolEntities then add one more partial class along with custom method.



namespace WebApplication10
{
public partial class SchoolEntities
{
public string MyCustomMethod()
{
return "Hey I am returned from Custom Method ";
}

}
}

Modifying Service Class

Once you have added custom method in partial entity class, next you need to add method in service class (.svc) with WebGet attribute

image

Above function is creating instance of entity class and making a call to custom method. Custom method will be called at URL baseaddress/service.svc/CustomMethod

Putting all together service class with one custom method will be as below,


using System.Data.Services;
using System.Data.Services.Common;
using System.ServiceModel.Web;

namespace WebApplication10
{
public class WcfDataService1 : DataService<SchoolEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{

config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}


[WebGet]
public string CustomMethod()
{
SchoolEntities entities = new SchoolEntities();
return entities.MyCustomMethod();
}



}
}


Calling custom method at client side

To call custom method at client side you need to make call to Execute method on instance of Entity class. Execute is a generic method and it takes return type of custom method as parameter. In this case return type of custom method is string. Execute method returns IEnuramble. As the parameter to Execute method you need to pass URI od custom method.

image

We can print returned value from custom method from WCF Data Service as below,


using System;
using ConsoleApplication32.ServiceReference1;

namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
SchoolEntities entities = new SchoolEntities
(new Uri("http://localhost:2580/WcfDataService1.svc/"));
var result = entities.Execute<string>
(new Uri("http://localhost:2580/WcfDataService1.svc/CustomMethod"));

foreach (var r in result)
{
Console.WriteLine(r.ToString());
}
Console.ReadKey(true);


}
}
}


On running of above code you should be getting output as below,

clip_image002

In this way you can work with custom method in WCF Data service. I hope this post is useful. Thanks for reading.

2 responses to “How to work with Custom Method in WCF Data Service?”

  1. […] How to work with Custom Method in WCF Data Service? and Flyout control in Windows 8 HTML JavaScript Metro Application and Code analysis using Telerik JustCode (Dhananjay Kumar) […]

  2. […] How to work with Custom Method in WCF Data Service? […]

Leave a comment

Create a website or blog at WordPress.com