<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>REST</title>
        <link>http://www.jamescbender.com/bendersblog/category/18.aspx</link>
        <description>REST</description>
        <language>en-US</language>
        <copyright>James Bender</copyright>
        <managingEditor>james@jamescbender.com</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <item>
            <title>Getting Started with WCF Part 8 &amp;ndash; REST Services: the Basics and Building Your First REST Service</title>
            <link>http://jamescbender.com/bendersblog/archive/2011/12/06/getting-started-with-wcf-part-8-ndash-rest-services-the.aspx</link>
            <description>&lt;p&gt;Previous posts in this series:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/03/06/working-with-wcf-part-one-introduction-and-your-first-service.aspx"&gt;Working with WCF: Part One – Introduction and Your First Service&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/03/17/working-with-wcf-part-two-ndash-your-first-host-and.aspx"&gt;Working with WCF: Part Two – Your First Host and A Bit About Configuration&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/04/02/working-with-wcf-part-three-ndash-connecting-to-your-service.aspx"&gt;Working with WCF: Part Three – Connecting To Your Service With A Client pt. 1&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/04/23/working-with-wcf-part-three-ndash-connecting-to-your-service-again.aspx"&gt;Working with WCF: Part Three – Connecting To Your Service With A Client pt. 2&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.jamescbender.com/bendersblog/archive/2010/05/07/working-with-wcf-part-four-ndash-data-contracts.aspx"&gt;Working with WCF: Part Four - Data Contracts&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/06/15/working-with-wcf-part.aspx"&gt;Working with WCF: Part Five - Message Contracts&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/06/23/working-with-wcf-part-six-ndash-fault-contracts.aspx"&gt;Working with WCF: Part Six – Fault Contracts&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/07/15/working-with-wcf-part-seven---whats-the-deal-with.aspx"&gt;Working with WCF: Part Seven – What’s the deal with REST?&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;… annnnnnd we’re back.&lt;/p&gt;  &lt;p&gt;Based on the strengths of REST outlined in my last post in this series, it would be a good tool to add to our WCF service arsenal. Our web-based bagel bakery has been very successful, prompting the opening of new locations. Some of these locations are the traditional “counter service” type of bagel store that people are more accustomed to. Others are a café style with an expanded menu, fancy coffee and Internet access. &lt;/p&gt;  &lt;p&gt;Our web page has a list of these locations, but as anyone invested in mobile development  will tell you, you can’t have a successful business these days without having a smartphone application.A feature we would like for our smart phone application is a store locator. For now, we just want to provide a list of stores that the application can consume, but we don’t want this information hard-coded into the application, as it’s possible that any day now a large amount of venture capital will fall into our lap and we’ll want to open more stores&lt;/p&gt;  &lt;p&gt;The best solution is to provide a web service that allows the application to access a current list of open stores at any time. To keep this service light weight and ensure that it is open to any smart phone platform we have decided to make this a REST based service. With WCF this is very easy.&lt;/p&gt;  &lt;h1&gt;Step One: The Service Contract&lt;/h1&gt;  &lt;p&gt;SOAP and REST based services are very different. The fact is that WCF was built on the idea of building SOAP based services and that is the focus of the classes in the System.ServiceModel assembly. In .NET 3.5 Microsoft added REST functionality to WCF in the form of the System.ServiceModel.Web assembly. This new assembly added some pieces that we will need to create our REST service. &lt;/p&gt;  &lt;p&gt;Consider this service contract:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb.png" width="592" height="211" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;By now you should be able to easily determine that any service that implements this IStoreLocatorService contract must expose an action method called GetStores that returns a list of some Store objects. As is this is a standard WCF service an action that responds to an HTTP POST and returns a list of Stores in a SOAP message is created.&lt;/p&gt;  &lt;p&gt;In order to tell WCF that we want this to be a REST based service we need to add an additional attribute to our operation:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_3.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_3.png" width="593" height="241" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The inclusion of the WebGet attribute in our service contract tells WCF that, depending on some configuration details, a service that implements this contract must be able to respond to an HTTP call with the GET verb (hence the “Get” in “WebGet”). For now don’t worry about the other verbs, we’ll get to them later.&lt;/p&gt;  &lt;p&gt;There are several arguments we can pass into the WebGet attribute to change how this method behaves, but for now we’re just using the UriTemplate option. The UriTemplate tells WCF what the URI that access this service should look like. In this case we’re telling WCF that if the base address of our service is “www.WebBagels.com” and the client uses that as it’s URI it will access this method via a pattern match. If we were to make a small change to the UriTemplate for our service….&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_4.png" width="592" height="250" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;… our service will now be called if the client attempts to access the www.WebBagles.com/Stores URI.&lt;/p&gt;  &lt;p&gt;That’s pretty nifty, but what happens if a user is specifically looking for one of our “café” stores? Users are going to want a way to filter this list. And besides, one of the big benefits of REST is supposed to be the cacheable, easy to understand hierarchical way of representing data, right? &lt;/p&gt;  &lt;p&gt;WCF makes if very easy, again using a simple pattern matching algorithm to enable us to have dynamic arguments embedded in our URI. We’ll add a new method to our service contract that enables users to search for specific store types:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_5.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_5.png" width="614" height="270" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Our new method still responds to an HTTP GET, but we’ve added a second UriTemplate with a different patters to match. In our GetStoresByStoreType method we are now looking for a URI that includes the store type of the stores we are searching for.&lt;/p&gt;  &lt;p&gt;The first thing I had to do was create a new method called GetStoresByStoreType that took a string parameter of storeType. It’s important to note that when you are using parameterized URI’s like we are here, the arguments for your method MUST all be strings; no other types are allowed. The reason is that the URI itself is a string and all constituent parts of it must be treated as strings as well. You can cast them all you want once they are passed into your method, but the method prototype must define these as strings. Aside from that you need to make sure that the token you are using in the UriTemplate (in this case {storeType}) matches the name of the parameter you want to map it to, including capitalization. Order is unimportant and if the URI has a token that does not map to a parameter on your method WCF will just ignore it. On the other hand if you have a parameter for your method that does not map to a token in your UriTemplate .NET will throw an error since it is missing parameters for the method call.&lt;/p&gt;  &lt;p&gt;Based on this service contract we will have two URI’s that our service is able to respond to:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;www.WebBagles.com/Stores – Results in a call to the GetStores method, returns a list of all stores. &lt;/li&gt;    &lt;li&gt;www.WebBagles.com/Stores/{storeType} – Results in a call to the GetStoresByStoreType method, returns a filtered list of stores. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;One last thing to notice here: The names of the .NET methods in our service contract has no bearing on or relationship to our URI’s. We could call those two methods anything we want (provided it’s a name .NET deems legal) and as long as our URI templates don’t change our clients would never know. This means that once we expose an API we can refactor internally all we want so long as we don’t change (mess up) those UriTemplates and break the API.&lt;/p&gt;  &lt;p&gt;By default WCF returns POX (Plain Ol’ XML) for it’s REST methods. Most users, myself included, would rather consume this information as JSON (JavaScript Object Notation) in many, if not most situations. For one thing as a web developer I find myself taking more and more advantage of AJAX calls from the browser. In these cases I would much rather consume the native JSON object than futz with the JavaScript XML parser. &lt;/p&gt;  &lt;p&gt;Having said that there are a lot of applications that consume REST services from C# or VB.NET and XML seems to be the more popular choice in those languages. As a result I like to write my REST services in a way that enables the users to determine how they get the data back (POX or JSON). For this service I’ll do that by adding a two new operation contracts to my IStoreLocatorService service contract; one to return all stores as a list of JSON objects and another to return list of stores filtered by store type as JSON objects:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_6.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_6.png" width="614" height="448" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;First let’s compare the contract for GetStores to the one for GetStoresInJson. The UriTemplate for GetStoresInJson is the same as the UriTemplate for the GetStores contract, except that I’ve added “/js” to the end. When WCF sees a request for a URI that looks like “~/Stores/js” it knows to route that action to the GetStoresInJson method since the incoming URI matches the pattern defined in the WebGet attribute of the GetStoresInJson method. Similarly, the GetStoresByStoreTypeInJson method is able to respond to requests that have URI’s with the pattern of “~/Stores/{storeType}/js” as opposed to “~/Stores/{storeType}” mapping those actions to the GetStoresByStoreType method which returns POX.&lt;/p&gt;  &lt;p&gt;The other change to note is that unlike the previous methods we worked with (GetStores and GetStoresByStoreType) we’ve added a new parameter to the WebGet attribute; ResponseFormat. As you can probably infer from it’s name, it’s used to tell WCF how you want the response from the service formatted. As I mentioned before the default is POX, but by specifying a value for the ResponseFormat (in this case WebMessageFormat.Json) we can change how the data is returned. As you may guess there is a corollary parameter, RequestFormat that specifies how the service should expect incoming request data to be formatted, but I’ll get into that in a future post.&lt;/p&gt;  &lt;p&gt;I’ve talked quite a bit about the service contact side of building these REST based services, but I haven’t touched on the implementation. That’s because the vast majority of the work to make these WCF services RESTful is done in the service contract. Here are the implementations for GetStores and GetStoresInJson:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_7.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_7.png" width="614" height="293" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As you can see, there isn’t really much to these implementations. The methods just return a list of Store objects. The GetStoresByStoreType and GetStoresByStoreTypeInJson just use a LINQ query to get the specific stores that match the store type out of the ListOfStores list. WCF, via the definition in the service contract will serialize this as either POX or JSON when it’s returned.&lt;/p&gt;  &lt;h1&gt;Step Two: The Data Contract&lt;/h1&gt;  &lt;p&gt;So how about that Store object? Like all complex structures that are sent received via WCF services, the easiest way to deal with it is to create a Data Contract. The Data Contract for our REST based service is very similar to the data contracts we’ve created for the other services in this series:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_8.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_8.png" width="614" height="571" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Like the previous data contracts this one has a class level attribute of DataContract and each property that I want to expose via the service is decorated with a DataMember attribute. In this case I have added a Namespace parameter to the DataContract attribute at the class level. I’ll explain why and more about namespaces in a future post.&lt;/p&gt;  &lt;h1&gt;Step Three: Updating the Host&lt;/h1&gt;  &lt;p&gt;The previous services in this series were all SOAP services. As a result we were able to use the ServiceHost class to host those services. Since REST services live by a different set of rules than SOAP services, particularly the use of parameterized URI’s REST services needs a host with slightly different capabilities than the one used for SOAP services. &lt;/p&gt;  &lt;p&gt;WCF provides the WebServiceHost class for hosting REST based web services. As you can see from this code snippet, it’s just as easy to use as the ServiceHost class:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_9.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_9.png" width="614" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Now that we have our hosting situation sorted out, it’s time to look as our configuration.&lt;/p&gt;  &lt;h1&gt;Step: Four: Updating the configuration&lt;/h1&gt;  &lt;p&gt;Since we’ve added a new service to our solutions, we need to add a new service section to our configuration. We already did this for our SOAP service and our REST service is actually pretty easy by comparison:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_10.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_10.png" width="614" height="122" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The address attribute identifies the base address for our service. The URI Template from our service contract is appended to this base URI in the configuration. This means that the URI we will use to get to the list of stores is http://localhost:8733/Stores. To search by store type the URI would be http://localhost:8733/Stores/&amp;lt;store type being searched&amp;gt;. &lt;/p&gt;  &lt;p&gt;For binding we’ll need to use the webHttpBinding. This is a REST specific binding and will tell WCF to build a channel stack that includes the serializers for POS and JSON messages. Contract behaves exactly the same way it has for our SOAP services.&lt;/p&gt;  &lt;p&gt;This configuration works for our self-hosted service. For hosting REST based WCF services in IIS you will need to make a couple more small changes. I’ll cover those configuration changes in a few posts when I cover deployment.&lt;/p&gt;  &lt;h1&gt;Step Five: Testing and Profit&lt;/h1&gt;  &lt;p&gt;To test our SOAP services we either had to use the WCF test client of write an application that created a proxy object that would serialize and send SOAP messages as POST actions and deserialzie the SOAP messages that were returned. Since REST based services are HTTP based and don’t reply with complex SOAP messages we can easily test our service in any web browser. To test our service I’ll start by launching the hosting application:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_11.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_11.png" width="612" height="94" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I’ll fire up Chrome (I prefer Chrome over IE for this, you’ll see why in a minute) and put the URI for our Stores REST service into the address bar (this is the base address from the configuration of the endpoint plus the URI template from the service contract) and hit Enter. Since our service just returns POX, we have not problem being able to view the message in Chrome (or any browser for that matter) without our faces melting:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_12.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_12.png" width="612" height="390" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;To filter this down by store type we can add the store type to the URI as proscribed by the URI template in the operation contract for the GetStoresByStoreType method of our Service Contract:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_13.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_13.png" width="612" height="278" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;WCF mapped the URI with the store type data to the correct method that filtered the store data by store type. What happens if we supply a store type that has no stores:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_14.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_14.png" width="614" height="160" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We simply got an empty list back. This is the preferred way REST services should handle this type of situation; if there is no data that satisfies the query then return an empty result.&lt;/p&gt;  &lt;p&gt;Say we want JSON. As we provided additional Operation Contracts that specified JSON as the response format when “/js” was added to our URI we simply need to add that to our current URI to change the format of the response data:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_15.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_15.png" width="614" height="158" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Line-breaks aside, JSON is a pretty easy format to understand and digest. Incidentally, JSON services are why I prefer to use Chrome for this kind of testing as opposed to Internet Explorer. For whatever reason IE does not want to render JSON and instead will make you save it to a file which you then will have to open in a text editor:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_16.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com/BendersBlog/Images/Blog/Getting-Started-with-WCF-Part-XREST-Serv_B0F8/image_thumb_16.png" width="612" height="48" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The end result is the same, but I appreciate that Chrome doesn’t make me go through this extra step to see my data.&lt;/p&gt;  &lt;p&gt;You may have noticed that we didn’t work much with query strings here. That will be covered in a future post.&lt;/p&gt;  &lt;p&gt;In the next post we’ll see how to send data to our service with the POST verb and lean how to test our services with one of my favorite all time developer tools.&lt;/p&gt;  &lt;p&gt;Code on!&lt;/p&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/88.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2011/12/06/getting-started-with-wcf-part-8-ndash-rest-services-the.aspx</guid>
            <pubDate>Wed, 07 Dec 2011 00:20:18 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/88.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2011/12/06/getting-started-with-wcf-part-8-ndash-rest-services-the.aspx#feedback</comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/88.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Working with WCF Part Seven - What's the deal with REST?</title>
            <link>http://jamescbender.com/bendersblog/archive/2010/07/15/working-with-wcf-part-seven---whats-the-deal-with.aspx</link>
            <description>&lt;p&gt;Previous posts in this series:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/03/06/working-with-wcf-part-one-introduction-and-your-first-service.aspx"&gt;Working with WCF: Part One – Introduction and Your First Service&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/03/17/working-with-wcf-part-two-ndash-your-first-host-and.aspx"&gt;Working with WCF: Part Two – Your First Host and A Bit About Configuration&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/04/02/working-with-wcf-part-three-ndash-connecting-to-your-service.aspx"&gt;Working with WCF: Part Three – Connecting To Your Service With A Client pt. 1&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/04/23/working-with-wcf-part-three-ndash-connecting-to-your-service-again.aspx"&gt;Working with WCF: Part Three – Connecting To Your Service With A Client pt. 2&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.jamescbender.com/bendersblog/archive/2010/05/07/working-with-wcf-part-four-ndash-data-contracts.aspx"&gt;Working with WCF: Part Four - Data Contracts&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/06/15/working-with-wcf-part.aspx"&gt;Working with WCF: Part Five - Message Contracts&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://jamescbender.com/bendersblog/archive/2010/06/23/working-with-wcf-part-six-ndash-fault-contracts.aspx"&gt;Working with WCF: Part Six – Fault Contracts&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We’re gonna step out of the whole Web Bagels/WCF demo thing on this post so I can give you a brief primer on Representational State Transfer (REST).&lt;/p&gt;  &lt;p&gt;The good news is that if you are reading this blog then you have used REST. The reason is that REST is the architectural basis for the Internet. When you opened your web browser of choice and typed &lt;a href="http://www.jamescbender.com"&gt;www.jamescbender.com&lt;/a&gt; into the address bar and hit ENTER, the browser issued a request to the access resource located at &lt;a href="http://www.jamescbender.com"&gt;www.jamescbender.com&lt;/a&gt; (whatever that may be) using the REST GET verb. In fact, if you’ve ever navigated to any website you’ve seen REST in action. That’s the basis of REST; clients make requests to servers using a Uniform Resource Identifier (URI) which, depending on which REST verb was used in the request, returns, created, alters or deletes the resource at the location specified by the URI. It’s that simple.&lt;/p&gt;  &lt;h1&gt;URI’s and You&lt;/h1&gt;  &lt;p&gt;There are two components of a REST request; the URI and the verb. &lt;/p&gt;  &lt;p&gt;The URI is probably the easiest for most people to get their heads around. It’s the location of the resource on the Internet that we want to get/change/delete. We’ve all used websites before, and we generally access them by a URL. You can think of a REST services URI as it’s location. But there is a bit more to it than that. Consider the following list of URI:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;www.GloboCorp.com/Departments &lt;/p&gt;    &lt;p&gt;www.GloboCorp.com/Departments/Sales &lt;/p&gt;    &lt;p&gt;www.GloboCorp.com/Departments/Sales/Employees/JohnDoe &lt;/p&gt;    &lt;p&gt;www.GloboCopr.com/Departments/Sales/Employees/search?name=JohnDoe &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The first thing to notice about all of these is that they are human readable. You may know nothing about Globo Corp., but you know that it has departments, one of those departments is Sales and it has an employee named John Doe. A big part of our ability to understand that is the hierarchy that the information is presented in. When creating URI’s for REST services it’s important to think about how you want your information to be accessed and conceptualized. In this case it makes sense to have the list of Departments as one of the “top level” branches in the hierarchy. For another company it may make sense to have Employees be the top level. It all depends on what your data is like and how you want it presented.&lt;/p&gt;  &lt;p&gt;Another thing to note is the “Universal” aspect of the URI. These are simply locations on the Internet. There is no special decoder ring needed to access these; any application that can use HTTP, and can handle the content type of the response (text, a picture, a sound file, etc.), can use the resources located at these addresses. You could embed these URI’s in a document and anyone reading that document, assuming they had some sort of HTTP capable web browser and access to the Internet could get the resource that resides at that location.&lt;/p&gt;  &lt;p&gt;Finally, since these are simply URI’s, and there is nothing special about them, they can be cached like any other URI. This is a big advantage over the SOAP based services we’ve been using up till now. Since there’s a certain amount of “magic” that happens when a SOAP service is invoked caching is difficult, usually involving some sort of customization on the host. Since REST is URI based most web servers provide the ability to cache right out of the box.&lt;/p&gt;  &lt;h1&gt;VERB – That’s what’s happening.&lt;/h1&gt;  &lt;p&gt;Once we have our resource location we need to specify what we want to do with the resource. These is where the HTTP Request Method, a.k.a. the  REST verb come into play. There are a variety of REST verbs available. The four are the most commonly used and you will use in your everyday like are the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Get – returns the resource at the URI’s location &lt;/li&gt;    &lt;li&gt;Post – creates a resource at the URI’s location &lt;/li&gt;    &lt;li&gt;Put – alters the resource at the URI’s location &lt;/li&gt;    &lt;li&gt;Delete – delete the resource at the URI’s location &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In addition to these four, you may start to encounter the HEAD verb as more service providers have begun implementing it. HEAD returns metadata about the resource at the location that you would get if you sent a Get verb.&lt;/p&gt;  &lt;p&gt;The benefit of the verb paradigm in REST is that we have a standardized interface. Unlike SOAP based services we aren’t creating our own verbs like GetEmployee or CreateSale. Being able to understand the information hierarchy behind our URI’s and having a standard list of methods (verbs) we can use on those resources makes developing clients that use REST services much easier and more intuitive.&lt;/p&gt;  &lt;h1&gt;POX and JSON&lt;/h1&gt;  &lt;p&gt;So now we know how to request resources from REST based web services. But what are we getting back. In the Web Bagels example we’ve been using SOAP based web services, which in fact return SOAP messages. REST doesn’t use SOAP. So what are our options?&lt;/p&gt;  &lt;h1&gt;POX&lt;/h1&gt;  &lt;p&gt;One option for our response is Plain Ol’ XML (POX). Like SOAP, our data comes back as an XML stream, That’s really where the similarity ends. Unlike SOAP, POX is very small, terse and easy to read.  For example, consider this SOAP message:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com//BendersBlog/Images/Blog/WorkingwithWCFPartSevenWhatsthedealwithR_8AB2/image.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com//BendersBlog/Images/Blog/WorkingwithWCFPartSevenWhatsthedealwithR_8AB2/image_thumb.png" width="642" height="277" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now check out the exact same message in it’s POX version:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com//BendersBlog/Images/Blog/WorkingwithWCFPartSevenWhatsthedealwithR_8AB2/image_3.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com//BendersBlog/Images/Blog/WorkingwithWCFPartSevenWhatsthedealwithR_8AB2/image_thumb_3.png" width="642" height="91" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Wow! Three things were accomplished here. First, and most strikingly it’s a MUCH smaller message. As a result it’s become much more human-friendly. Additionally we’ve trimmed a lot of the complexity that comes with SOAP out of the process. We do need to keep in mind that the simplicity comes at a cost; when we removed the SOAPyness from our message we lost some security as well as some other WS-* abilities like addressing, routing, reliability and anything involving workflow. More on that in a bit. But what we do have is a simple XML document that can be easily understood, parsed and acted upon. &lt;/p&gt;  &lt;h1&gt;JSON&lt;/h1&gt;  &lt;p&gt;I’ve you’ve been doing “Web 2.0” stuff you’ve no doubt heard of Asynchronous JavaScript and XML, better known as AJAX. AJAX is all the rage with web developers who want to provide a good user experience on the web by making several “smaller” calls to a back-end web service and using that result to change part of the page instead of doing a full page refresh. These keeps the user from having to sit and wait for the whole page to be re-served and re-rendered when only a small part of the page is changing. Most of these calls are coming from client side JavaScript and in the “dark ages” you would get back an XML document that you would have to parse before you can use. &lt;/p&gt;  &lt;p&gt;JavaScript Object Notation or JSON was defined in 2001, but didn’t really gain steam till 2005 when large web properties like Yahoo, Google and Microsoft embraced it. The idea was that since we’re already using JavaScript to make these calls, and JavaScript is a dynamic scripting language, why not just return the text that corresponds to a JavaScript object? This made life a lot easier for the client developers, and once libraries to quickly format messages for JSON it made life just as easy for service developers too. An example of the JSON equivalent of the previous message would look something like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jamescbender.com//BendersBlog/Images/Blog/WorkingwithWCFPartSevenWhatsthedealwithR_8AB2/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.jamescbender.com//BendersBlog/Images/Blog/WorkingwithWCFPartSevenWhatsthedealwithR_8AB2/image_thumb_4.png" width="642" height="106" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As you can see JSON shares a lot of characteristics of POX; both are simple, compact and human readable. It’s also instantly consumable by JavaScript so there’s no need to parse it, pull it apart and put it in an object; it’s already in one. Once you get this back from your service you can use it like any other object in your client side JavaScript.&lt;/p&gt;  &lt;h1&gt;What of SOAP?&lt;/h1&gt;  &lt;p&gt;The resurgence of REST does not mean the death of SOAP. Yes, SOAP is complex. But that complexity brings power. For the Enterprise, SOAP is not going away. There are still too many things that business cares about that are easier to do in SOAP than with REST-based services. Things like transactions, reliable messaging, workflows and orchestrations, message routing and custom security. SOAP excels at these and that is why you need to know (and care) about it. &lt;/p&gt;  &lt;p&gt;What is changing is how you architect and plan your service infrastructure. Specifically where you use SOAP/REST based services. Personally, I’ll start from the standpoint of making something a REST service until a requirement arises that can be more quickly, easily and reliably done with SOAP. Then I’ll switch to SOAP. An emerging pattern many have seen that follows this line of thinking is that we are starting to see REST services deployed along the edges of a service architecture, in places where outside client need to have easy and interoperable access to our data. While SOAP services are still very much relied on in the center of the service architecture, where things like sessions, long running workflows, more advanced security and services busses are needed. Of course the standard Architect mantra of “It depends” comes into play.&lt;/p&gt;  &lt;p&gt;Next time we’ll use WCF to add some REST-based services to our Web Bagels application. &lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://jamescbender.com/bendersblog/archive/2010/07/15/working-with-wcf-part-seven---whats-the-deal-with.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://jamescbender.com/bendersblog/archive/2010/07/15/working-with-wcf-part-seven---whats-the-deal-with.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/83.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2010/07/15/working-with-wcf-part-seven---whats-the-deal-with.aspx</guid>
            <pubDate>Thu, 15 Jul 2010 14:49:31 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/83.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2010/07/15/working-with-wcf-part-seven---whats-the-deal-with.aspx#feedback</comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/83.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Thoughts on the WCF REST Starter Kit</title>
            <link>http://jamescbender.com/bendersblog/archive/2008/12/22/thoughts-on-the-wcf-rest-starter-kit.aspx</link>
            <description>&lt;p&gt;One of the announcements that was kinda missed at PDC was the release of the &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/netframework/cc950529.aspx"&gt;WCF REST Starter Kit&lt;/a&gt;. In brief, the REST Starter Kit provides a was for developers to “jump-start” the development of REST services in WCF. &lt;/p&gt;
&lt;h2&gt;The Premise&lt;/h2&gt;
&lt;p&gt;In .NET  3.0 WCF allowed developers to abstract the way a service communicates with the world from the way the functionality of the service was implemented. Developers designing services in a “contract first” manner were able to specify minimum requirement and rules for their services. Provided that the communication medium (transport and binding) was able to provide the service with the facilities that it needed, that service could be invoked via that medium. For example, a service could expose both HTTP based and TCP based endpoints without having to change the functionality of the service. In WCF there is not a concept of a “web service”: there are just services. Some are available via HTTP over the web.&lt;/p&gt;
&lt;p&gt;WCF also provided developers with ways to customize and extend the way the service communicated and behaved at runtime via custom channels and custom behaviors.&lt;/p&gt;
&lt;p&gt;What it didn’t do very well was provide a way for developers to easily create REST based services. But that changed with the release of .NET 3.5. Using the WebGet and WebInvoke attributes in the Service Contract gave developers a way to write REST services in WCF&lt;/p&gt;
&lt;h2&gt;The Problem&lt;/h2&gt;
&lt;p&gt;For all it’s power, configurability and extensibility WCF has always had a bit of an image problem. Most developers view it as difficult to work with, difficult to understand and tied to a obnoxiously verbose configuration model. The inclusion of REST abilities in 3.5 did nothing to help do away with this perception.&lt;/p&gt;
&lt;h2&gt;The Solution&lt;/h2&gt;
&lt;p&gt;Enter the WCF REST Starter Kit. The Starter Kit provides a set of templates for creating a variety of REST-based services including Singleton Services (exposing a single resource on the server), Collection Service (exposing a list of resources/data), POX Service and projects for ATOM feeds and ATOM publishing protocols. The starter kit also comes with a new assembly called Microsoft.ServiceModel.Web which contains an API the provides a lot of out-of-the box functionality to support REST services including customized help pages, customized caching, easier ways to return exceptions, URI templates, and the ability to use request and response headers.&lt;/p&gt;
&lt;p&gt;The long and the short of it is that you select a project template based on what kind of service you want to write and then add the appropriate functionality to a few methods and bang, you’ve got a service. &lt;/p&gt;
&lt;h2&gt;The Good&lt;/h2&gt;
&lt;p&gt;The starter kit comes with templates for most kind of REST based services you will need to write. While a basic knowledge of REST and how routing works (if you’ve done any significant MVC work you’ll be fine) you can get a REST based service up and running in a matter of minutes with data represented as both XML and JSON. Little real WCF knowledge is required to simply get a service up and running. &lt;/p&gt;
&lt;p&gt;The API extensions will offer something to those more experienced with WCF by providing a means to perform some customization without having to write a custom behavior or channel. &lt;/p&gt;
&lt;h2&gt;The Bad&lt;/h2&gt;
&lt;p&gt;It started with the install. Part of the starter kit is a bunch of Visual Studio content. The installer kept giving me a Null Reference Exception. I found a workaround &lt;a href="http://madridcentral.com/blogs/chris/archive/2008/10/30/wcf-rest-starter-kit-preview-1.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The classes created by the starter kit are designed for ease of implementation, not for extensibility. Customization involves a lot of friction. If you need to do anything beyond a basic REST implementation you will need to acquaint yourself with the object model that the starter kit uses, which can be a little overly complex. If you need to do complex routing or create a service to provide more than one data type you’ll need a level of knowledge about REST in WCF that the start kit is supposed to insulate you from.&lt;/p&gt;
&lt;p&gt;Another issue I’ve encountered is that when I try to rename the Service.svc file in the project generated by the Starter Kit I get an “… operation not completed. Unspecified error.” error.&lt;/p&gt;
&lt;h2&gt;In the End&lt;/h2&gt;
&lt;p&gt;I’m in favor of a REST Starter Kit, or ANYTHING for that matter that increases adoption of WCF. I think the Starter Kit is a good late-beta/1.0 release, but it’s got a ways to got before I consider it a major part of my tool-box.&lt;/p&gt;
&lt;p&gt;Code On!&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjamescbender.com%2fbendersblog%2farchive%2f2008%2f12%2f22%2fthoughts-on-the-wcf-rest-starter-kit.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjamescbender.com%2fbendersblog%2farchive%2f2008%2f12%2f22%2fthoughts-on-the-wcf-rest-starter-kit.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/37.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2008/12/22/thoughts-on-the-wcf-rest-starter-kit.aspx</guid>
            <pubDate>Tue, 23 Dec 2008 01:53:13 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/37.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2008/12/22/thoughts-on-the-wcf-rest-starter-kit.aspx#feedback</comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/37.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Announcement from PDC...</title>
            <link>http://jamescbender.com/bendersblog/archive/2008/10/26/announcement-from-pdc.aspx</link>
            <description>&lt;p&gt;In Roc Jabocs pre-PDC session he announced that the WCF REST Starter kit has been replaced on &lt;a target="_blank" href="http://codeplex.com/"&gt;Codeplex&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It's not official till tomorrow, and a quick search on Codeplex didn't have it listed. Guessing it will be up tomorrow. Be sure to check it out when it's available!&lt;/p&gt;&lt;img src="http://jamescbender.com/bendersblog/aggbug/30.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>James Bender</dc:creator>
            <guid>http://jamescbender.com/bendersblog/archive/2008/10/26/announcement-from-pdc.aspx</guid>
            <pubDate>Sun, 26 Oct 2008 23:33:22 GMT</pubDate>
            <wfw:comment>http://jamescbender.com/bendersblog/comments/30.aspx</wfw:comment>
            <comments>http://jamescbender.com/bendersblog/archive/2008/10/26/announcement-from-pdc.aspx#feedback</comments>
            <wfw:commentRss>http://jamescbender.com/bendersblog/comments/commentRss/30.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
