{"id":261,"date":"2013-04-12T13:27:54","date_gmt":"2013-04-12T13:27:54","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=261"},"modified":"2013-05-02T15:08:56","modified_gmt":"2013-05-02T15:08:56","slug":"wcf-basics","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/wcf-basics\/","title":{"rendered":"WCF &#8211; Basics"},"content":{"rendered":"<p>I&#8217;ve used WCF (and the previous incarnation of web services in .NET) on and off for a while. However once the inital work to set the application up is completed I tend to forget much of what I&#8217;ve done to concentrate on the actual application. This is one of the great things with WCF &#8211; it just works once set-up.<\/p>\n<p>So this post is going to be written as a quick refresher. <\/p>\n<p><strong>ABC<\/strong><\/p>\n<p>One of the key things to remember with WCF is the the setting up is pretty much all about the ABC&#8217;s<\/p>\n<ul>\n<li><strong>A<\/strong> is of <strong>Address<\/strong>. In other words what&#8217;s the location (or endpoint) of the service.<\/li>\n<li><strong>B<\/strong> is of <strong>Binding<\/strong>. How do we connect to the endpoint, i.e. what&#8217;s the protocol etc.<\/li>\n<li><strong>C<\/strong> is of <strong>Contract<\/strong>. The application\/programming interface to the web service, i.e. what does it do ?<\/li>\n<\/ul>\n<p><strong>Contracts<\/strong><\/p>\n<p>Simply put, creating an interface and decorating the interface with [ServiceContract] will mark out an interface as a service contract. Marking the methods that you want to  publish with [OperationContract] i.e.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ServiceContract]\r\npublic interface IPlantService\r\n{\r\n   &#x5B;OperationContract]\r\n   List&lt;Plant&gt; GetPlants();\r\n   \/\/ and so on\r\n}\r\n<\/pre>\n<p>where we&#8217;re passing our own complex types back (in the above we have the Plant class) here we mark the class with [DataContract] attribute and each property we wish to publish are marked with [DataMember] as per<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;DataContract]\r\npublic class Plant\r\n{\r\n   &#x5B;DataMember]\r\n   public int Id { get; set; }\r\n   &#x5B;DataMember]\r\n   public string CommonName { get; set; }\r\n   \/\/ ... and so on\r\n}\r\n<\/pre>\n<p>Don&#8217;t forget to include the System.ServiceModel assembly and System.Runtime.Serialization.<\/p>\n<p><strong>Hosting a service outside of IIS<\/strong><\/p>\n<p>Whilst IIS offers a scaleable, consistent and secure architecture for hosting WCF web services, one might prefer to host via a console app or Windows service for example. The following is a simple example of the code needed to get WCF self hosted in a console application.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nclass Program\r\n{\r\n   static void Main(string&#x5B;] args)\r\n   {\r\n      ServiceHost serviceHost = new ServiceHost(typeof(PlantService));\r\n      serviceHost.Open();\r\n      Console.WriteLine(&quot;Service Running&quot;);\r\n      Console.ReadLine();\r\n      serviceHost.Close();\r\n   }\r\n}\r\n<\/pre>\n<p><strong>Configuring the ABC&#8217;s<\/strong><\/p>\n<p>Configuration of the ABC&#8217;s can be accomplished through code or more usually through configuration. I&#8217;m not going to dig deep into configuration but instead point to something I&#8217;ve only recently been made aware of. Having always edited the app.config by hand.<\/p>\n<p>It&#8217;s slightly strange in Visual Studio 2010 how you enable this option but go to Tools | WCF Service Configuration Editor then close it. Now if you right mouse click to get the context menu on the App.config file you&#8217;ll see the <em>Edit WCF Configuration option<\/em>. Now you can configure WCF via this UI.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve used WCF (and the previous incarnation of web services in .NET) on and off for a while. However once the inital work to set the application up is completed I tend to forget much of what I&#8217;ve done to concentrate on the actual application. This is one of the great things with WCF &#8211; [&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":[3,2,11],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","category-wcf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/261","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=261"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}