In my previous post, Simple GET REST Service with WCF, I detailed how to build a simple web service with WCF that routed a simple request and constructed a REST/POX response. As part of the release of the ASP.NET 3.5 Extensions yesterday, the previously named "Project Astoria" was released as ADO.NET Data Services. There's some details here about what all ADO.NET Data Services include. The feature I want to outline in this post is the feature that can serialize these entities into the industry standard AtomPub format.
After installing the Extensions CTP, you'll see some new options when you are creating a new web project.
Choose ASP.NET 3.5 Extensions Web Site. Once the site is created, add a new item and choose "ADO.NET Data Service" from the Item Template window.
After that, you'll want to create a simple LINQ to SQL class. You can see my post here about how to do that. For this example, I'm going to connect to the NorthwindEF sample database and just drag the Customers table onto the designer surface.
Once I've created the ADO.NET Data Service and the LINQ to SQL classes, then I'll need to wire up my simple data source into the service. This is really simple. First thing to do is to put your DataContext class created by the LINQ to SQL step into the service class declaration.
What looked like this:
Should now look like this:
After that, you'll want to make sure that you provide access to your data source, as it is off by default. In this example, I'll give access to every data entity in the data context object (all classes that implement IQueryable<T> by using the wildcard. After making these changes, your service class should look like this:
That's it. If you fire up your service, you should see something like this:
From there, you can do all sorts of fun stuff with the URL:
http://localhost:50508/Astoria/MySimpleService.svc/Customers yields all customers:
http://localhost:50508/Astoria/MySimpleService.svc/Customers('ALFKI') returns just that customer with that primary key.
http://localhost:50508/Astoria/MySimpleService.svc/Customers?$filter=CompanyName%20eq%20'Alfreds%20Futterkiste' returns all customers with that specific customer id.
There's a bunch more stuff you can do with the URI addressing schemes, such as ordering a and paging. Fantastic!
You can't beat that. 10 minute fully functional REST web service. This has such great potential.