With WCF Data Services (formerly known as ADO.NET Data Services) we can publish any datasource with one or more IQueryable properties to the web. By using a with the Open Data Protocol (OData) formatted url we can query, update, insert and delete data from and to the database. The data output is lightweight AtomPub or JSON, instead of SOAP.
In this example we use the Hotels database from previous examples this time accessed with the Entity Framework. To host the WCF Data Service we need an ASP.NET Web Application. We call it HotelData in this case. To this ASP.NET site we add a new WCF Data Service called Service.svc. We add a reference to our Entity Framework project.
We open the Service.svc file and configure the DataService initialization with the Entity Framework ObjectContext we want to use; in this case HotelEntities.In the InitializeService method we specify the access rules for the entities we want to expose. In our example we only want to read some hotel and city data, so we set the SetEntitySetAccessRule to AllRead for both entities.
using System.Data.Services; using System.Data.Services.Common; using Remondo.Entities; namespace HotelData { public class Service : DataService<HotelEntities> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("Hotels", EntitySetRights.AllRead); config.SetEntitySetAccessRule("Cities", EntitySetRights.AllRead); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } } }
Since we do not have a repository in the example, we need to copy the connection string from our Entity Framework project to the Web.config of our ASP.NET Website. And we’re done… ;-)
<configuration> <connectionStrings> <add name="HotelEntities" connectionString="metadata=res://*/DataModel.csdl|r ..... <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> </system.serviceModel> </configuration>
If we browse to the Service.svc on our website we get an AtomPub Workspace with links to our published collections; Hotels and Cities.
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> <service xml:base="http://localhost:11017/Service.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app"> <workspace> <atom:title>Default</atom:title> <collection href="Cities"> <atom:title>Cities</atom:title> </collection> <collection href="Hotels"> <atom:title>Hotels</atom:title> </collection> </workspace> </service>
From there we can go to a specific entity, lets say, a City with an ID of 2 by browsing to http://[servername]/Service.svc/Cities(2). This City happens to be Amsterdam.
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> <entry xml:base="http://localhost:11017/Service.svc/" 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"> <id>http://localhost:11017/Service.svc/Cities(2)</id> <title type="text"></title> <updated>2012-04-15T10:55:52Z</updated> <author> <name /> </author> <link rel="edit" title="City" href="Cities(2)" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Hotels" type="application/atom+xml;type=feed" title="Hotels" href="Cities(2)/Hotels" /> <category term="RemondoModel.City" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:ID m:type="Edm.Int32">2</d:ID> <d:Name>Amsterdam</d:Name> </m:properties> </content> </entry>
The highlighted url above is an association from City to Hotels. So browsing to this link gives us an Atom feed with a resultset of all the hotels in Amsterdam stored in the database. We have only three hotels registered in this city, being Hotel de L’Europe, The Exchange and the InterContinental Amstel.
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> <feed xml:base="http://localhost:10013/Hotel.svc/" 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"> <title type="text">Hotels</title> <id>http://localhost:10013/Hotel.svc/Cities(2)/Hotels</id> <updated>2012-04-15T11:03:38Z</updated> <link rel="self" title="Hotels" href="Hotels" /> <entry> <id>http://localhost:10013/Hotel.svc/Hotels(5)</id> <title type="text"></title> <updated>2012-04-15T11:03:38Z</updated> <author> <name /> </author> <link rel="edit" title="Hotel" href="Hotels(5)" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/City" type="application/atom+xml;type=entry" title="City" href="Hotels(5)/City" /> <category term="RemondoModel.Hotel" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:ID m:type="Edm.Int32">5</d:ID> <d:Name>Hotel de L'Europe</d:Name> <d:City_ID m:type="Edm.Int32">2</d:City_ID> <d:Stars m:type="Edm.Int32">5</d:Stars> </m:properties> </content> </entry> <entry> <id>http://localhost:10013/Hotel.svc/Hotels(7)</id> <title type="text"></title> <updated>2012-04-15T11:03:38Z</updated> <author> <name /> </author> <link rel="edit" title="Hotel" href="Hotels(7)" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/City" type="application/atom+xml;type=entry" title="City" href="Hotels(7)/City" /> <category term="RemondoModel.Hotel" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:ID m:type="Edm.Int32">7</d:ID> <d:Name>The Exchange</d:Name> <d:City_ID m:type="Edm.Int32">2</d:City_ID> <d:Stars m:type="Edm.Int32">4</d:Stars> </m:properties> </content> </entry> <entry> <id>http://localhost:10013/Hotel.svc/Hotels(8)</id> <title type="text"></title> <updated>2012-04-15T11:03:38Z</updated> <author> <name /> </author> <link rel="edit" title="Hotel" href="Hotels(8)" /> <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/City" type="application/atom+xml;type=entry" title="City" href="Hotels(8)/City" /> <category term="RemondoModel.Hotel" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <content type="application/xml"> <m:properties> <d:ID m:type="Edm.Int32">8</d:ID> <d:Name>InterContinental Amstel</d:Name> <d:City_ID m:type="Edm.Int32">2</d:City_ID> <d:Stars m:type="Edm.Int32">4</d:Stars> </m:properties> </content> </entry> </feed>
Of course there’s a lot more we can do with OData and WCF Data Services, but these are the basics.









