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.
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*"/>
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
</configuration>
After that I added a Global.asax. And changed it to this.
Imports System.Web.SessionState
Imports Funq
Imports ServiceStackModel.Request
Imports ServiceStack.WebHost.Endpoints
Public Class Global_asax
Inherits System.Web.HttpApplication
Public Class HelloAppHost
Inherits AppHostBase
Public Sub New()
MyBase.New("Plant Web Services", GetType(PlantService).Assembly)
End Sub
Public Overrides Sub Configure(container As Container)
Routes.Add(Of PlantRequest)("/plant").Add(Of PlantRequest)("/plant/{Name}")
End Sub
End Class
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim apphost = New HelloAppHost()
apphost.Init()
End Sub
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.
```vbnet
Namespace Request
Public Class PlantRequest
Public Property Name As String
End Class
End NameSpace```
```vbnet
Namespace Response
Public Class PlantResponse
Public Property Name As String
End Class
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.
```vbnet
Imports ServiceStackModel.Response
Imports ServiceStackModel.Request
Imports ServiceStack.ServiceHost
Public Class PlantService
Implements IService(Of PlantRequest)
Public Function Execute(request As PlantRequest) As Object Implements ServiceStack.ServiceHost.IService(Of PlantRequest).Execute
Return New PlantResponse With {.Name = "Name: " & request.Name}
End Function
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.
Imports ServiceStackModel.Request
Imports ServiceStackModel.Response
Imports ServiceStack.ServiceClient.Web
Module Module1
Sub Main()
Dim client = New JsonServiceClient("http://localhost:3318")
Dim response = client.Send(Of PlantResponse)(New PlantRequest With {.Name = "World!"})
Console.WriteLine(response.Name)
Console.ReadLine()
End Sub
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.
Imports EasyHttp.Http
Module Module1
Sub Main()
Dim http = New HttpClient()
http.Request.Accept = HttpContentTypes.ApplicationJson
Dim response = http.Get("http://localhost:3318/plant/test?format=json")
Dim plant = response.DynamicBody
Console.WriteLine(plant.Name)
Console.ReadLine()
End Sub
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.