Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « VB.Net: Guess the resultAn extension method for getting rid of those cross thread UI calls in winforms »
    comments

    Introduction

    This morning I read a tweet by @ONE75 (Stijn Volders) about a blogpost by Phillip Haydon (@philliphaydon).

    The blogpost is about servicestack.

    Service Stack is a high-performance .NET web services framework (including a number of high-performance sub-components: see below) that simplifies the development of XML, JSON, JSV and WCF SOAP Web Services. For more info check out servicestack.net.

    So I went out and tried it.

    The server

    First I set out to create the server. So I created an Empty ASP.Net web application.

    And then you add the nuget package.

    I used Install-package ServiceStack

    And I changed the web.config to this.

    1. <?xml version="1.0"?>
    2.  
    3. <!--
    4.   For more information on how to configure your ASP.NET application, please visit
    5.   <a href="http://go.microsoft.com/fwlink/?LinkId=169433">
    6. http://go.microsoft.com/fwlink/?LinkId=169433</a>
    7.   -->
    8.  
    9. <configuration>
    10.     <system.web>
    11.         <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    12.     </system.web>
    13.   <system.web>
    14.     <httpHandlers>
    15.       <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*"/>
    16.     </httpHandlers>
    17.   </system.web>
    18.  
    19.   <!-- Required for IIS 7.0 -->
    20.   <system.webServer>
    21.     <validation validateIntegratedModeConfiguration="false" />
    22.     <handlers>
    23.       <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    24.     </handlers>
    25.   </system.webServer>
    26. </configuration>

    After that I added a Global.asax. And changed it to this.

    1. Imports System.Web.SessionState
    2. Imports Funq
    3. Imports ServiceStackModel.Request
    4. Imports ServiceStack.WebHost.Endpoints
    5.  
    6. Public Class Global_asax
    7.     Inherits System.Web.HttpApplication
    8.  
    9.     Public Class HelloAppHost
    10.         Inherits AppHostBase
    11.  
    12.         Public Sub New()
    13.             MyBase.New("Plant Web Services", GetType(PlantService).Assembly)
    14.         End Sub
    15.        
    16.         Public Overrides Sub Configure(container As Container)
    17.             Routes.Add(Of PlantRequest)("/plant").Add(Of PlantRequest)("/plant/{Name}")
    18.         End Sub
    19.     End Class
    20.  
    21.     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    22.         Dim apphost = New HelloAppHost()
    23.         apphost.Init()
    24.     End Sub
    25.  
    26. End Class

    The routes are optional, I think, but useful later on in this blogpost.

    I then added a classlibrary for my model.

    Which contains 2 files.

    The Request and the response.

    1. Namespace Request
    2.     Public Class PlantRequest
    3.         Public Property Name As String
    4.     End Class
    5. End NameSpace
    1. Namespace Response
    2.     Public Class PlantResponse
    3.         Public Property Name As String
    4.     End Class
    5. End NameSpace

    And now for the most important class, the service itself.
    Very simple service, return the name with the name of the request attached to that.

    1. Imports ServiceStackModel.Response
    2. Imports ServiceStackModel.Request
    3. Imports ServiceStack.ServiceHost
    4.  
    5. Public Class PlantService
    6.     Implements IService(Of PlantRequest)
    7.  
    8.     Public Function Execute(request As PlantRequest) As Object Implements ServiceStack.ServiceHost.IService(Of PlantRequest).Execute
    9.         Return New PlantResponse With {.Name = "Name: " & request.Name}
    10.     End Function
    11.  
    12. End Class

    If you run this you should get this.

    Because of the routes we can also surf to http://localhost:3318/plant/test and get this.

    But now we also want to get that data in our client code.

    Servicestack client

    You can use the servicestack clients if you so desire. For this to work your client will also need a reference to your model.

    And you will need the nuget package ServiceStack.Common.

    Making it work is fairly easy.

    1. Imports ServiceStackModel.Request
    2. Imports ServiceStackModel.Response
    3. Imports ServiceStack.ServiceClient.Web
    4.  
    5. Module Module1
    6.  
    7.     Sub Main()
    8.         Dim client = New JsonServiceClient("http://localhost:3318")
    9.         Dim response = client.Send(Of PlantResponse)(New PlantRequest With {.Name = "World!"})
    10.         Console.WriteLine(response.Name)
    11.         Console.ReadLine()
    12.     End Sub
    13.  
    14. End Module

    You send an object of the request type and get one back of the response type. Easy as pie.

    The easyhttp client

    Of course this is just a webservice so you can use other clients to get your result.

    I used easyhttp.

    And I used the routes I configured earlier to get my result.

    1. Imports EasyHttp.Http
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Dim http = New HttpClient()
    7.         http.Request.Accept = HttpContentTypes.ApplicationJson
    8.         Dim response = http.Get("http://localhost:3318/plant/test?format=json")
    9.         Dim plant = response.DynamicBody
    10.         Console.WriteLine(plant.Name)
    11.         Console.ReadLine()
    12.     End Sub
    13.  
    14. End Module

    With this approach I don't need a reference to the Model I created earlier I just use Dynamic objects.

    The code

    You can find the code on github

    Conclusion

    Servicestack is easy to setup and get working. Getting the results from your services is easy to. All this provided that you have the web development things installed for Visual studio ;-), which I hadn't.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    1 comment

    Comment from: Ed [Visitor]
    Ed Thanks so much for posting this. I was having a hard time getting it to work but now it makes a lot more sense!
    11/28/12 @ 12:31

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)