{"id":324,"date":"2013-05-02T14:46:48","date_gmt":"2013-05-02T14:46:48","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=324"},"modified":"2015-07-02T15:30:38","modified_gmt":"2015-07-02T15:30:38","slug":"quickstart-wcf-service-and-client","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/quickstart-wcf-service-and-client\/","title":{"rendered":"Quickstart WCF service and client"},"content":{"rendered":"<p>I&#8217;ve already got a post on WCF basics, but I had to quickly knock together a WCF service and client to try something out today and I thought it&#8217;d be useful to list the steps to create a real bare bones service and client in a single post. <\/p>\n<p>Let&#8217;s begin with the service itself. I want to create a self hosted service running from a console app. so I can easily debug and control everything, so<\/p>\n<p>1. Open a new instance of Visual Studio<br \/>\n2. Create a new console application.<br \/>\n3. Copy the following into the Main method<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nServiceHost serviceHost = new ServiceHost(typeof(MyService));\r\nserviceHost.Open();\r\nConsole.WriteLine(&quot;Service Running&quot;);\r\nConsole.ReadLine();\r\nserviceHost.Close();\r\n<\/pre>\n<p>4. Add the reference to System.ServiceModel as well as a valid app.config. For example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;system.serviceModel&gt;\r\n   &lt;behaviors&gt;\r\n      &lt;serviceBehaviors&gt;\r\n         &lt;behavior name=&quot;&quot;&gt;\r\n            &lt;serviceMetadata httpGetEnabled=&quot;false&quot; \/&gt;\r\n            &lt;serviceDebug includeExceptionDetailInFaults=&quot;false&quot; \/&gt;\r\n         &lt;\/behavior&gt;\r\n      &lt;\/serviceBehaviors&gt;\r\n   &lt;\/behaviors&gt;\r\n   &lt;services&gt;\r\n      &lt;service name=&quot;WcfTest.MyService&quot;&gt;\r\n         &lt;endpoint address=&quot;&quot; binding=&quot;netTcpBinding&quot; bindingConfiguration=&quot;&quot;\r\n                   contract=&quot;WcfTest.IMyService&quot;&gt;\r\n            &lt;identity&gt;\r\n               &lt;dns value=&quot;localhost&quot; \/&gt;\r\n             &lt;\/identity&gt;\r\n         &lt;\/endpoint&gt;\r\n         &lt;endpoint address=&quot;mex&quot; binding=&quot;mexTcpBinding&quot; bindingConfiguration=&quot;&quot;\r\n                    contract=&quot;IMetadataExchange&quot; \/&gt;\r\n         &lt;host&gt;\r\n            &lt;baseAddresses&gt;\r\n               &lt;add baseAddress=&quot;net.tcp:\/\/localhost:8732\/MyService\/&quot; \/&gt;\r\n            &lt;\/baseAddresses&gt;\r\n         &lt;\/host&gt;\r\n      &lt;\/service&gt;\r\n   &lt;\/services&gt;\r\n&lt;\/system.serviceModel&gt;  \r\n<\/pre>\n<p>5. Create an interface with at least one method, as we&#8217;re calling the service MyService, let&#8217;s call the interface IMyService. You&#8217;ll need at least one method and the interface shold have a ServiceContract attribute and the method have an OperationContract, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ServiceContract]\r\npublic interface IMyService\r\n{\r\n   &#x5B;OperationContract]\r\n   void SayHello(string name);\r\n}\r\n<\/pre>\n<p>6. Implement the interface for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyService : IMyService\r\n{\r\n   public void SayHello(string name)\r\n   {\r\n      Console.WriteLine(&quot;Hello &quot; + name);\r\n   }\r\n}\r\n<\/pre>\n<p>6. Run the application<\/p>\n<p>Now to the client&#8230;<\/p>\n<p>1. Open a new instance of Visual Studio<br \/>\n2. Create a console application<br \/>\n3. Add a service reference (use the url from the server, i.e. net.tcp:\/\/localhost:8732\/MyService\/mex from the service&#8217;s app.config)<br \/>\n4. Click Go<br \/>\n5. Click OK<br \/>\n6. Finally add the following code into the Main method<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar client = new MyServiceClient();\r\nclient.SayHello(&quot;Scooby Doo&quot;);\r\nclient.Close();\r\n<\/pre>\n<p>Now we have a working service and client.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve already got a post on WCF basics, but I had to quickly knock together a WCF service and client to try something out today and I thought it&#8217;d be useful to list the steps to create a real bare bones service and client in a single post. Let&#8217;s begin with the service itself. I [&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-324","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\/324","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=324"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":3254,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/324\/revisions\/3254"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}