public class WcfDataService1 : DataService< /* TODO: put your data source class name here */ >
{
// 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("MyEntityset", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
public class WcfDataService1 : DataService<Model1Container>
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetEntitySetAccessRule("Employee", EntitySetRights.AllRead);
config.SetEntitySetAccessRule("Department", EntitySetRights.AllRead);
Now let’s see how we can consume this data service. I am creating a console application and adding a service reference.
Now I am writing the following code.
using ConsoleApplication1.svcWcfDataService;
using System;
using System.Linq;
namespace ConsoleApplication1
class Program
static void Main(string[] args)
Model1Container modelContext = new Model1Container(new Uri("http://localhost:51639/WcfDataService1.svc/"));
var employees = from e in modelContext.Employees
select e;
foreach (var e in employees)
Console.WriteLine("{0}, {1}", e.LastName, e.FirstName);
Console.ReadLine();
I am creating a object of my data context. Now I am writing a LINQ query to retrieve the data. Behind the scene the LINQ query is transforming into URL syntax and executes HTTP methods such as “GET”,“POST” etc. based on my request.
I am attaching a sample project to MSDN Code Gallery, you can play around. Download Sample Happy Coding.