{"id":4375,"date":"2017-01-03T20:31:25","date_gmt":"2017-01-03T20:31:25","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4375"},"modified":"2017-01-03T20:31:25","modified_gmt":"2017-01-03T20:31:25","slug":"implementing-a-client-and-services-with-servicestack","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/implementing-a-client-and-services-with-servicestack\/","title":{"rendered":"Implementing a client and services with ServiceStack"},"content":{"rendered":"<p>ServiceStack, is a lightweight framework for writing web services. <\/p>\n<p><strong>Getting Started<\/strong><\/p>\n<p>Let&#8217;s just create a &#8220;default&#8221; service and client first.<\/p>\n<p>See <a href=\"http:\/\/docs.servicestack.net\/create-your-first-webservice\" target=\"_blank\">Create your first WebService<\/a> for information on creating your first service. I&#8217;ll replicate some of the steps here also&#8230;<\/p>\n<ul>\n<li>Using Visual Studio, Tools | Extensions and Updated, search for ServiceStack templates and install (if not already installed)<\/li>\n<li>Create a new ServiceStack ASP.NET Empty project (target .NET 4.6)<\/li>\n<li>Build and run<\/li>\n<\/ul>\n<p>By default this will create a service which accepts a Hello object and returns a HelloResponse (for reference, here&#8217;s those classes)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class HelloResponse\r\n{\r\n   public string Result { get; set; }\r\n}\r\n\r\n&#x5B;Route(&quot;\/hello\/{Name}&quot;)]\r\npublic class Hello : IReturn&lt;HelloResponse&gt;\r\n{\r\n   public string Name { get; set; }\r\n}\r\n<\/pre>\n<p><em>Note: The Hello class &#8220;implements&#8221; IReturn (which actually is just a marker interface, i.e. no methods) to give our client the ability to pass Hello objects (and hence have type safety etc.) to the service and inform the client what response to expect.<\/em><\/p>\n<p>We can test the service using our web browser, for example my server is running on the localhost on port 50251, so typing the URL <em>http:\/\/localhost:50251\/Hello\/Bob<\/em> (note: the Route attribute defines the &#8220;route&#8221; to the specific method etc. hence you can see how this maps to the URL we used) will result in the Hello class being run with the argument Bob as the Name. The response will be displayed in the browser, along the lines of Result <em>Hello, Bob!<\/em>. This is the same result we&#8217;ll be expecting from our client app.<\/p>\n<p>But how did we get the result &#8220;Hello, World!&#8221; from the server? This is where the MyService implementation in the generated server code comes in. When the server&#8217;s AppHost starts up it looks for services. These are then used to intercept\/generate calls and responses. See the code below (copied from the generated code)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyServices : Service\r\n{\r\n   public object Any(Hello request)\r\n   {\r\n      return new HelloResponse \r\n      { \r\n         Result = &quot;Hello, {0}!&quot;.Fmt(request.Name) \r\n      };\r\n   }\r\n}\r\n<\/pre>\n<p>Let&#8217;s create a client app.<\/p>\n<ul>\n<li>Create a Console application<\/li>\n<li>From the Package Manager Console, run Install-Package ServiceStack.Client<\/li>\n<li>Copy the Hello and HelloResponse classes from the service to the client<\/li>\n<li>In the Main method add the following\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar client = new JsonServiceClient(&quot;http:\/\/localhost:50251\/&quot;);\r\nvar resp = client.GetAsync(new Hello { Name = &quot;Bob&quot; })\r\n   .Success(tsk =&gt;\r\n   {\r\n      Console.WriteLine(tsk.Result);\r\n   })\r\n   .Error(e =&gt;\r\n   {\r\n      Console.WriteLine(e.Message);\r\n   });\r\n\r\nConsole.ReadLine();\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>For the client Api we could have called the service using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar resp = client.GetAsync&lt;HelloResponse&gt;(&quot;\/hello\/World&quot;);\r\n<\/pre>\n<p>if preferred, and this will do away for the need for the Hello class in the client.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ServiceStack, is a lightweight framework for writing web services. Getting Started Let&#8217;s just create a &#8220;default&#8221; service and client first. See Create your first WebService for information on creating your first service. I&#8217;ll replicate some of the steps here also&#8230; Using Visual Studio, Tools | Extensions and Updated, search for ServiceStack templates and install (if [&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":[139],"tags":[],"class_list":["post-4375","post","type-post","status-publish","format-standard","hentry","category-servicestack"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4375","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=4375"}],"version-history":[{"count":15,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4375\/revisions"}],"predecessor-version":[{"id":4540,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4375\/revisions\/4540"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}