WCF Data Service

On 19/08/2011, in WCF, by bcakiroglu

WCF Data Services enables you to create and consume Open Data Protocol (OData) services in an application. OData exposes data as resources that are addressable by URIs, allowing you to access and change data by using the semantics of representational state transfer (REST), specifically the standard HTTP verbs of GET, PUT, POST, and DELETE.

  • GET- To request a specific representation of a resource.
  • PUT- To create/ update a resource.
  • Delete- To delete a resource.
  • POST- Submits data to be processed by the identified resource.
  • HEAD- Similar to GET, but retrieves only header part and not the body.

Step 1. Obtain The WCFDataService Template If You Don’t Have One

You can find it by using visual studio extensions.

Step 2. Add “WCF Data Service” Project To The Solution

Step 3. Add An Entity Framework Model (ie: SAMPLEModel.edmx) To The “WCF Data Service” Project

Make database connection, create database entities, CRUD operations.(ie:Employees)

Step 4. Configure The WCF Data Service

1. In the WcfDataService.cs file, put your data source class name here, which is obtained from entity framework model (ie: SAMPLEModel.SAMPLEEntities).

2. Allow access to the entities exposed by the Data Service. By default, access is not allowed to all entity sets. Access must be allowed for each entity set.

public class WcfDataService: DataService<SAMPLEModel.SAMPLEEntities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

http://localhost:4777/WcfDataService.svc/Employees //Gets all employees

http://localhost:4777/WcfDataService.svc/Employees(3) //Gets specific employee with id:3

Step 5. Test The Service With A Client Console Application

Add WcfDataService as service reference.

 class Program
    {
        static void Main(string[] args)
        {
            ServiceRef.SAMPLEEntities context = new ServiceRef.SAMPLEEntities (new Uri("http://localhost:4777/WcfDataService.svc"));

            //Example:
            Employee emp= context.Employees.Where(e => e.EmployeeID == 149).SingleOrDefault(); // To select single
            emp.FirstName = "Burcak";

            // To Update: context.UpdateObject(emp); 
            // To Add: context.AddObject("Employees",emp);
            // To Delete: context.DeleteObject(emp);
            context.SaveChanges();
        }
    }
Tagged with:  

4 Responses to WCF Data Service

  1. Raju Golla says:

    Hi

    Recently I read a book and done simple WCF service consumed by WPF client.
    Here are authors words “Most business applications must work with data contained in a database. Clients need to be able to perform CRUD (create, read, update, and delete) operations on the data. While you can support these operations using the HTTP SOAP based WCF services discussed thus far, you need to write a lot of code to hook up the database layer and expose it through the operations exposed by the WCF service. This is
    where WCF Data Services can help. WCF Data Services is a framework that enables you to easily create services to expose and consume data over the Web”.

    So far I have been writing methods in DAL to interact with database to perform CRUD operations. After writing above mentioned service I realised that WCF Data service API has provided CRUD methods without need from the developer to write these methods.

    My query is ‘would you consider WCF data services a way farward and advanced than standard WCF services?’ . I use WCF services to perform CRUD operations mostly.

    Your insight and valid input is highly appreciated.

    Regards,
    Raju

    • bcakiroglu says:

      Hi Raju,

      Actually in enterprise level softwares, we do not prefer to use rapid development approaches, such as RIA, WCFDataService, LinqToSQL…etc. Because of their limitations. They are designed to develop quick applications without consuming time, usally they are not so much flexible and based on a specific technology.

      As a result it depends on your software’s level.

  2. anilbabu says:

    How to perform CRUD operations in WCF REST service And also tell me How to consume this REST service in ASP.NET webpages…………..Give me one Simple EXAMPLE Help me

    All the operations Perform using database?

    pls help me……

    • bcakiroglu says:

      If you are asking how to perform CRUD operations with WCF Data Service:

      ServiceRef.SAMPLEEntities context = new ServiceRef.SAMPLEEntities (new Uri(“http://localhost:4777/WcfDataService.svc”));
      //Example:
      Employee emp= context.Employees.Where(e => e.EmployeeID == 149).SingleOrDefault(); // To select single
      emp.FirstName = “Burcak”;

      // To Update: context.UpdateObject(emp);
      // To Add: context.AddObject(“Employees”,emp);
      // To Delete: context.DeleteObject(emp);
      context.SaveChanges();

      If you prefer to use WCF REST instead of WCFData Service:

      http://burcakcakiroglu.com/?p=2503

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

iBlog by PageLines