WCF Data Services Intro
WCF Data Services Intro
Introduction
Agenda
• Purpose
– Expose Data as RESTful Web service
• History
– Project Astoria at Mix 07 (Mar 2007)
– V1.0 Released as ADO.NET Data Services in .NET 3.5 SP1 (Aug 2008)
– V1.5 Update released (Jan 2010)
– V4.0 WCF Data Services in .NET 4.0 (Apr 2010)
– V5.0 Released in (Apr 2012)
• OData protocol used by the service
WCF data problems
NorthwindDataService.svc
<%@ ServiceHost Language="C#"
Factory="System.Data.Services.DataServiceHostFactory"
Service="NorthwindDataService" %>
Enity Framework
using System.Data.Services; Object Context
public class NorthwindDataService
: DataService< NorthwindModel.NorthwindEntities >
{
}
Data Service shall provide
access to all entity sets in
object context
navigating to svc
file returns top-
level ATOM feed
Data Services and ATOM
http://localhost:1234/NorthwindService/Service.svc
/Customers
/Customers('ALFKI') key values and
/Customers('ALFKI')/Orders properties
/Customers('ALFKI')/Orders(1)/Product
Query string parameters
[DataServiceKey("Name")]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
• In a custom model
[EntityPropertyMapping("Name",
SyndicationItemProperty.Title,
SyndicationTextContentKind.Plaintext,
keepInContent: true)]
• In an Entity model
<edmx:Edmx Version="3.0"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<EntityType Name="Title">
<Property Name="BookTitle"
m:FC_TargetPath="SyndicationTitle"
m:FC_ContentKind="text"
m:FC_KeepInContent="true"/>
Retrieving data from WCF Data Service
using System;
using System.Net;
using System.IO;
generated
client
entities
DataServiceContext
IQueryable<Product> query =
from p in ctx.Products
where p.Category.CategoryName == "Beverages"
orderby p.ProductName
select p;
DataServiceQuery dsQuery = (DataServiceQuery)query;
Console.WriteLine("\n{0}", dsQuery.RequestUri);
.../Products()?$filter=Category/CategoryName eq 'Beverages'
&$orderby=ProductName
Client change-tracking
product.Category = confections;
ctx.SetLink(product, "Category", confections);
ctx.SaveChanges();
Save changes
SaveChangesOptions Remark
None One HTTP request for each modification
Batch Single HTTP Post request containing all
updates is sent to service
ReplaceOnUpdate Use HTTP verb "POST" instead of "MERGE"
ContinureOnError Multiple requests are sent. When request
causes error, further requests are still sent.
Summary