<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shawson&#039;s Code Blog &#187; WCF</title>
	<atom:link href="http://codeblog.shawson.co.uk/tag/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeblog.shawson.co.uk</link>
	<description>development notes for my failing memory</description>
	<lastBuildDate>Tue, 15 May 2012 15:22:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Getting started with WCF and REST web services</title>
		<link>http://codeblog.shawson.co.uk/getting-started-with-wcf-and-rest-web-services/</link>
		<comments>http://codeblog.shawson.co.uk/getting-started-with-wcf-and-rest-web-services/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 09:03:07 +0000</pubDate>
		<dc:creator>shawson</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.shawson.co.uk/codeblog/?p=465</guid>
		<description><![CDATA[I&#8217;m building some REST WCF services at the moment and found this great guide online www.robbagby.com/rest/rest-in-wcf-blog-series-index The HiREST stuff shows you how to create fully fledged REST services utilising all of the HTTP verbs. Heres a quick summary of the most useful bits; The service itself is just added to the project as a normal [...]


Related posts:<ul><li><a href='http://codeblog.shawson.co.uk/wcf-services-with-json-and-soap-endpoints/' rel='bookmark' title='WCF Services with both JSON and SOAP endpoints'>WCF Services with both JSON and SOAP endpoints</a></li>
<li><a href='http://codeblog.shawson.co.uk/properties-getting-k__backingfield-appended-to-their-name-in-the-wsdl-file/' rel='bookmark' title='Properties getting &#8220;k__BackingField&#8221; appended to their name in the WSDL file'>Properties getting &#8220;k__BackingField&#8221; appended to their name in the WSDL file</a></li>
<li><a href='http://codeblog.shawson.co.uk/making-your-wcf-service-compatible-with-legacy-net-1-1-applications/' rel='bookmark' title='Making your WCF Service compatible with legacy .net 1.1 applications'>Making your WCF Service compatible with legacy .net 1.1 applications</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m building some REST WCF services at the moment and found this great guide online <a href="http://www.robbagby.com/rest/rest-in-wcf-blog-series-index">www.robbagby.com/rest/rest-in-wcf-blog-series-index</a></p>
<p>The HiREST stuff shows you how to create fully fledged REST services utilising all of the HTTP verbs.</p>
<p>Heres a quick summary of the most useful bits;</p>
<p>The service itself is just added to the project as a normal WCF service.  The following needs adding to the Web.config to setup the end </p>
<p>points;</p>
<pre class="brush: xml">
&lt;system.serviceModel&gt;
  &lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name=&quot;AJAXFriendly&quot;&gt;
        &lt;enableWebScript /&gt;
      &lt;/behavior&gt;
      &lt;behavior name=&quot;RESTFriendly&quot;&gt;
        &lt;webHttp /&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
&lt;/behaviors&gt;

&lt;services&gt;
    &lt;service      name=&quot;MyNamespace.AjaxServiceClassName&quot;&gt;
      &lt;endpoint   address=&quot;&quot;
        behaviorConfiguration=&quot;AJAXFriendly&quot;
        binding=&quot;webHttpBinding&quot;
        contract=&quot;CatalogService&quot; /&gt;
    &lt;/service&gt;
    &lt;service      name=&quot;MyNamespace.RESTServiceClassName&quot;&gt;
      &lt;endpoint   address=&quot;&quot;
        behaviorConfiguration=&quot;RESTFriendly&quot;
        binding=&quot;webHttpBinding&quot;
        contract=&quot;MyNamespace.IRESTServiceInterfaceName&quot; /&gt;
    &lt;/service&gt;
  &lt;/services&gt;
&lt;/system.serviceModel&gt;
</pre>
<p>Remember the class names in the web.config need to be fully qualified.</p>
<p>An example class for passing around the service;</p>
<pre class="brush: csharp">
[DataContract]
public class ProductData
{
    [DataMember]
    public int ProductId;

    [DataMember]
    public string ProductName;

    [DataMember]
    public string Description;

    [DataMember]
    public decimal Price;

    [DataMember]
    public string ProductImage;
}
</pre>
<p>The service itself is just a normal WCF service so consists of its usual two parts- the interface and the class.  Here&#8217;s an example interface </p>
<p>demoing the various access methods and return serialisations;</p>
<pre class="brush: csharp">
    [ServiceContract]
    public interface IRESTServiceInterfaceName
    {
        // responds to a GET request to www.mysite.com/Servicename.svc/products/
        // return is serialised to JSON
        [OperationContract]
        [WebGet(UriTemplate = &quot;products/&quot;, ResponseFormat = WebMessageFormat.Json)]
        List&lt;FlotChartSeries&gt; GetProducts();

        // responds to a GET request to www.mysite.com/Servicename.svc/products/{product_id}
        // return is serialised to JSON
        [OperationContract]
        [WebGet(UriTemplate = &quot;products/{product_id}&quot;, ResponseFormat = WebMessageFormat.Json)]
        List&lt;FlotChartSeries&gt; GetProductById(string product_id);

        // responds to a GET request to www.mysite.com/Servicename.svc/products?brand={brand}
        // return is serialised to JSON
        [OperationContract]
        [WebGet(UriTemplate = &quot;products?brand={brand}&quot;, ResponseFormat = WebMessageFormat.Json)]
        List&lt;FlotChartSeries&gt; GetProductsByBrand(string brand);

	// responds to a POST, but still passing a bunch of parameters into the querystring
	// no return type
        [WebInvoke(Method = &quot;POST&quot;, UriTemplate = &quot;product/{product_id}/rate?score={score}&quot;)]
        [OperationContract]
        void PostProductAppRating(string product_id, string score);

	// accepts a POST with a JSON encoded product data, to allow for an update
	// no return
	[WebInvoke(Method = &quot;POST&quot;, UriTemplate = &quot;product/{product_id}&quot;, RequestFormat = WebMessageFormat.Json)]
	void PostProductUpdate(string product_id, ProductData data);

	//responds to a PUT- passing a querystring variable for the filename- it expects the body of the push request to be a file stream
	// no return type
        [WebInvoke(Method = &quot;PUT&quot;, UriTemplate = &quot;products/{product_id}/desktopimage?filename={file_name}&quot;)]
        [OperationContract]
        void PutProductImage(string product_id, string file_name, Stream fileContents);

    }
</pre>
<p>Note the variables in the curly braces must match the real parameters to the method signitures for the value to be automatically mapped.</p>
<p>Responses are generally done using HTTP response codes.  At the top of each method you need to grab the current WebOperationContext which </p>
<p>will allow you to set the return codes;</p>
<pre class="brush: csharp">
    WebOperationContext ctx = WebOperationContext.Current;

    ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;		// .. if everything is ok!
    ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;	// on error
    ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Created;	// after an insert

    // all of the http response codes are mapped here

    ctx.OutgoingResponse.SetStatusAsNotFound(); // if record cannot be found or similar
</pre>


<p>Related posts:<ul><li><a href='http://codeblog.shawson.co.uk/wcf-services-with-json-and-soap-endpoints/' rel='bookmark' title='WCF Services with both JSON and SOAP endpoints'>WCF Services with both JSON and SOAP endpoints</a></li>
<li><a href='http://codeblog.shawson.co.uk/properties-getting-k__backingfield-appended-to-their-name-in-the-wsdl-file/' rel='bookmark' title='Properties getting &#8220;k__BackingField&#8221; appended to their name in the WSDL file'>Properties getting &#8220;k__BackingField&#8221; appended to their name in the WSDL file</a></li>
<li><a href='http://codeblog.shawson.co.uk/making-your-wcf-service-compatible-with-legacy-net-1-1-applications/' rel='bookmark' title='Making your WCF Service compatible with legacy .net 1.1 applications'>Making your WCF Service compatible with legacy .net 1.1 applications</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://codeblog.shawson.co.uk/getting-started-with-wcf-and-rest-web-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

