{"id":347,"date":"2013-05-09T10:28:25","date_gmt":"2013-05-09T10:28:25","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=347"},"modified":"2013-05-09T15:34:21","modified_gmt":"2013-05-09T15:34:21","slug":"wcf-rest","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/wcf-rest\/","title":{"rendered":"WCF Rest"},"content":{"rendered":"<p>Creating a REST style interface with WCF is fairly simple. The steps are listed briefly below, and in more depth below the list of steps.<\/p>\n<p>1. Create your interface\/service contract<br \/>\n2. Annotate the methods with WebGet or WebInvoke attributes<br \/>\n3. Create the implementation of the interface<br \/>\n4. Declare the endpoint within the configuration file (or in code). The binding should be webHttpBinding and should have an endpoint behaviour specificied with WebHttp<br \/>\n5. If need be declare the http baseAddress<\/p>\n<p>In more depth<\/p>\n<p>1. Create your interface\/service contract<br \/>\nand<br \/>\n2. Annotate the methods with WebGet or WebInvoke attributes<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ServiceContract]\r\npublic interface IRestService\r\n{\r\n   &#x5B;OperationContract]\r\n   &#x5B;WebGet(UriTemplate = &quot;projects&quot;)]\r\n   IList GetProjects();\r\n\r\n   &#x5B;OperationContract]\r\n   &#x5B;WebGet(UriTemplate = &quot;execute\/{projectName}&quot;)]\r\n   void Execute(string projectName);\r\n}\r\n<\/pre>\n<p>Note: Note to self. Don&#8217;t forget to put the ServiceContract attribute on the interface !<\/p>\n<p>I&#8217;ve implemented both methods a GET methods. Assuming a baseAddress of http:\/\/localhost:8733\/MyService\/ to call these methods through a web browser we can simply type http:\/\/localhost:8733\/MyService\/projects and http:\/\/localhost:8733\/MyService\/execute\/MyProject respectively.<\/p>\n<p>3. Create the implementation of the interface<\/p>\n<p>No need to list source code for this, simple implement the interface you created.<\/p>\n<p>4. Declare the endpoint within the configuration file (or in code). The binding should be webHttpBinding and should have an endpoint behaviour specificied with WebHttp<br \/>\nand<br \/>\n5. If need be declare the http baseAddress<\/p>\n<p>The key bit to getting everything to work is the configuration file. I&#8217;m self hosting for this example so implementing the code in the App.config<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;system.serviceModel&gt;\r\n\u00a0 \u00a0 &lt;behaviors&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;serviceBehaviors&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;behavior name=&quot;&quot;&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;serviceMetadata httpGetEnabled=&quot;false&quot; \/&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;serviceDebug includeExceptionDetailInFaults=&quot;false&quot; \/&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;\/behavior&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;\/serviceBehaviors&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;endpointBehaviors&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;behavior name=&quot;rest&quot;&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;webHttp\/&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;\/behavior&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;\/endpointBehaviors&gt;\r\n\u00a0 \u00a0 &lt;\/behaviors&gt;\r\n\u00a0 \u00a0 &lt;services&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;service name=&quot;StandardPlugins.MonitorService&quot;&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;endpoint address=&quot;&quot; binding=&quot;webHttpBinding&quot; bindingConfiguration=&quot;&quot;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 contract=&quot;StandardPlugins.IRestService&quot; behaviorConfiguration=&quot;rest&quot;&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;\/endpoint&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;host&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;baseAddresses&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;add baseAddress=&quot;http:\/\/localhost:8733\/MyService\/&quot; \/&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;\/baseAddresses&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;\/host&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;\/service&gt;\r\n\u00a0 \u00a0 &lt;\/services&gt;\r\n&lt;\/system.serviceModel&gt;\r\n<\/pre>\n<p>Note: The REST methods should be accessible via HTTP, hence the binding is webHttpBinding and the baseAddress is http. A behaviorConfiguration is required with the webHttp element.<\/p>\n<p>Further reading<\/p>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/magazine\/dd315413.aspx#id0070050\">http:\/\/msdn.microsoft.com\/en-us\/magazine\/dd315413.aspx#id0070050<\/a><br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/vstudio\/bb412172(v=vs.90).aspx\">http:\/\/msdn.microsoft.com\/en-us\/library\/vstudio\/bb412172(v=vs.90).aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a REST style interface with WCF is fairly simple. The steps are listed briefly below, and in more depth below the list of steps. 1. Create your interface\/service contract 2. Annotate the methods with WebGet or WebInvoke attributes 3. Create the implementation of the interface 4. Declare the endpoint within the configuration file (or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-347","post","type-post","status-publish","format-standard","hentry","category-wcf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=347"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}