Writing...Data Services

Coding and writing about OData and now Windows Azure Mobile Services...

MERGE Requests and the WCF Data Services Client

MERGE Requests and the WCF Data Services Client

  • Comments 1

I came across this interesting blog post by Alex van Beek the other day where he demonstrates how to reduce the size of the payload of MERGE requests to an OData service when using the WCF Data Services client. The OData protocol defines a HTTP MERGE action that enables a client to request updates to only specific properties of an entity, which sounds neat and perhaps a way to save bandwidth when updating an entity. But, as Alex points out, the WCF Data Services client library always includes all of the properties of an entity (at least the ones it knows about) in the request.

For example, the following is the payload of a MERGE request sent to a Northwind-based data service.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d=http://schemas.microsoft.com/ado/2007/08/dataservices 
    xmlns:m=http://schemas.microsoft.com/ado/2007/08/dataservices/metadata
    xmlns="http://www.w3.org/2005/Atom">
  <category scheme=http://schemas.microsoft.com/ado/2007/08/dataservices/scheme
    term="NorthwindModel.Customer" />
  <title />
  <author>
    <name />
  </author>
  <updated>2011-01-31T09:34:20.6690355Z</updated>
  <id>http://services.odata.org/northwind/Northwind.svc/Customers('ALFKI')</id>
  <content type="application/xml">
    <m:properties>
      <d:Address>Obere Str. 57</d:Address>
      <d:City>Berlin</d:City>
      <d:CompanyName>Alfreds Futterkiste</d:CompanyName>
      <d:ContactName>New Name</d:ContactName>
      <d:ContactTitle>Sales Representative</d:ContactTitle>
      <d:Country>Germany</d:Country>
      <d:CustomerID>ALFKI</d:CustomerID>
      <d:Fax>030-0076545</d:Fax>
      <d:Phone>030-0074321</d:Phone>
      <d:PostalCode>12209</d:PostalCode>
      <d:Region m:null="true" />
    </m:properties>
  </content>
</entry>

In this example, even though the only property that I changed on the client was ContactName, all of the properties were included by the client in the MERGE request. Why does the client include all of the properties in the MERGE request payload, even when those properties haven’t changed? As Alex correctly points out, it is because—unlike the ObjectContext in the ADO.NET Entity Framework—the DataServiceContext doesn’t have an ObjectStateManager-like component that tracks property-level changes on the client. All the client can do when an object has been marked as changed is send all of the properties that it knows about to the data service. (From the client’s perspective, the main difference between MERGE and PUT is that because a PUT is essentially an entity replace, any property values that the client doesn't know about are reset to their default values.) These MERGE request payloads can get large when the entity has BLOB properties (although you should really consider streaming BLOBs to an OData service—for more info see my Data Services Streaming Provider series).

By wrapping the DataServiceContext to implement his own property-level tracking, Alex’s example does reduce the payload of MERGE requests (PATCH requests, which will likely be supported in the future) to an OData service. If you think that this functionality should be added to the official WCF Data Services client, you can vote for it now at the WCF Data Services Feature Suggestions site.

Cheers!

Glenn Gailey

Leave a Comment
  • Please add 7 and 4 and type the answer here:
  • Post
  • Thanks for mentioning my blog post!

Page 1 of 1 (1 items)