find a location for property in a new city

Wednesday, 15 December 2010

WCF Data Services for using Entity Framework over REST web services

I spent some time looking into WCF Data Services 4.0 (AKA ADO.NET Data Services & Astoria) as a way to expose our Entity Data Model (EDM) over an easy to create set of RESTful / OData web services. I decided to document my findings.

WCF Data Services 4.0 is service that provides a way of exposing an EDM over RESTful (OData) web services using WCF. It means that if you have an EDM already built, the work is essentially done. The EDM can be exposed using WCF Data Services so that it can be queried directly by the client.

How to begin

I successfully set up some WCF Data Services to expose a Comment part of my model. This helped me. I did find difficulty due to our use of POCOs instead of raw Entity Framework's generated code but I found a post that helped which told me about a weird hack that overrides the CreateDataSource into the Data Service:
public class CommentsService : DataService<SocialEntities>
    protected override CommentingEntities CreateDataSource()
        var context = new CommentingEntities();
        var tracestring = context.CreateQuery<Comment>("CommentingEntities.Comments").ToTraceString();
        return context; 

Consuming the web services

A client can now access the data by requesting URLs. E.g. to get a list of messages or to get the message with ID 413 (the L is for long since I use long instead of int)

The querying gets a lot more complicated than that too. See how to use your browser to query the data model

There are many SDKs out there to begin developing against your WCF Data Services from a number of popular platforms since it is simply using the OData protocol (e.g. JavaScript, PHP, Ruby, Java, Objective-C, .NET).

Restricting use

You can restrict the number of results to return when returning a list of entities to, say 5, so the client can't overload the server/bandwidth.

You can also restrict access to select parts of your EDM to calm your security nerves too:
public static void InitializeService(DataServiceConfiguration config)
    config.SetEntitySetAccessRule("Comments", EntitySetRights.AllRead);

Evaluating when to use WCF Data Services

So it does look like a great way to use the existing EDM to instantly have a set of RESTful web services to consume. However I think I would only use this if the developers of the web service client were have knowledge of the data and are expecting to get stuck into some code when consuming our data services.

If were internally designing our own Silverlight apps then this is great. However, expecting an external user to write sometimes complex queries to get to our data using all of our business rules (e.g. IsDeleted = false && SiteID = 21) is a little much to be expected I feel.


So great when providing RESTful web services to a handful of specific clients who have knowledge of your data and business logic. Not so great for making a friendly API for the masses based on a complex EDM as you cannot expect them to understand your business rules as well as you do.

I am not claiming authority on this topic. I am just discussing my findings so far in the hope that you may find this useful. If you have anything you think I have missed please feel free to comment.

Follow britishdev on Twitter


  1. you can encapsulate and offer target data services

  2. Ensure business congruity. For instance, an unprotected PC can get to be contaminated and obliterate information that you have to maintain your business.

  3. So, if you want to cut the maximum ice, then you need professional SEO service to cater to your web design needs. SEO companies leave no stone unturned while delivering their service.
    learn web design

  4. The WCF programming model binds together Web Services, .NET Remoting, Distributed Transactions, and Message Queues into a solitary Service-arranged programming model for disseminated figuring.

  5. Let create a new world for yourselft by way take part in the games– sims 4 cheats in my site. Click link to visit site. Thank you so much.

  6. He's going to look into it for me. Yeah . . . don't speak too soon.Visit Websites

  7. Do you know a framework for paper writing? I need it for my college project

  8. شحن عفش من الدمام لمصر شحن عفش من الدمام لمصر
    ارخص نقل عفش بالمدينة المنورة ارخص نقل عفش بالمدينة المنورة
    شراء اثاث مستعمل بالدمام شراء اثاث مستعمل بالدمام
    شراء اثاث مستعمل بالاحساء شراء اثاث مستعمل بالاحساء
    شحن عفش من المدينة لمصر شحن عفش من المدينة لمصر

  9. Very helpful article, thanks for sharing.

  10. Packers and Movers Bangalore - Reliable and Verified Household Shifting Service Providers Give Reasonable ###Packers and Movers Charges. Cheap and Best Office Relocation Compare Quotation for Assurance for Local and Domestic House Shifting and Get estimates today to save upto 20%, ***Read Customer Reviews - @ Packers And Movers In Bangalore

  11. Packers and Movers Chennai Give Safe and Reliable ***Household Shifting Services in Chennai with Reasonable ###Packers and Movers Price Quotation. We Provide Household Shifting, Office Relocation, ✔✔✔ Local and Domestic Transportation Services, Affordable and Reliable Shifting Service Charges @ Packers And Movers Chennai

  12. CellAlpha is a One-Stop Platform where you can find genuine reviews. and free books, podcast, many more things

  13. Really this article is very useful and informative for everyone. And I hope you'll share more ideas and useful article with us, If you are looking Best Boot And Glove Dryers. Click Here

  14. Buy Best Quality certeza nebulizer price in pakistan at medisense with affordable prices. Shop Now.

  15. Buy comfortable baby products pakistan at medisense with best price. Shop Now.

  16. Buy Best eye care Products at medisense with best price. Shop Now.

  17. Excellent read, Positive site, where did u come up with the information on this posting? I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work,If anyone interested similar one's have a look here thanks.

    shih tzu puppies for sale near me
    buy puppy online