MonoTouch ServiceStack

Calling Web Services from MonoTouch

MonoTouch tutorial Step 1 - Getting Started

In this tutorial I will show how easy it is to create Web Services with Service Stack and call them from a MonoTouch application.

You can download the full source code for this tutorial at: http://servicestack.googlecode.com/files/RemoteInfo.zip.
Alternatively, you can browse all the source code online at: http://code.google.com/p/servicestack/source/browse/#svn/trunk/MonoTouch.Examples/RemoteInfo

For simplicity we'll start with an existing Mono Develop solution which has already been configured to host Web Services using Service Stack.
RemoteInfo is an example project that exposes information about a servers' file / directory structure via XML and JSON Web Services:

Clients

Example clients that call the Web Services

  • RemoteInfo.Tests.ConsoleClient - From a simple Console App
  • RemoteInfoClient - From a MonoTouch client

Hosts

Different ways to host ServiceStack Web Services

  • RemoteInfo.Host.Console - Running within a Console App (without a web server)
  • RemoteInfo.Host.Web - In a standard .NET Web Application

Application Code

  • RemoteInfo.ServiceInterface - Implementations for each Web Service
  • RemoteInfo.ServiceModel - Request and Response DataContract DTO's (also shared by .NET web service clients)

The configuration for the host projects are ultimately the same except that one is configured to be run in a Console Application while the other allows it to be hosted within a Web Server configured to run ASP.NET applications which include:
After the hosts are configured they no longer need to be modified as adding new Web Services only require adding to the 'Application Code' projects.

Configuring ServiceStack - Modifying AppHost.cs

The configuration of a ServiceStack Web Service Host is held in the AppHost.cs class. The contents of RemoteInfo's AppHost.cs is included below.
Configuring ServiceStack as seen in this example is pretty easy, generally the only things you need to do is:


The only thing left to do is to get your configuration to run on start-up which can be done in a Web Application done by adding protected void Application_Start(object sender, EventArgs e) { var appHost = new AppHost(); appHost.Init(); } to your Global.asax.cs.

If everything is configured properly you should be able to go to the base of your Web Application directory (RemoteInfo.Host.Web) in Terminal and run xsp from the Command Line:

Running XSP from the Command Line

If all went well and you didn't get any errors going to http://localhost:8080 should give you a splash page for your Web Services:

Web Services splash page

This splash page contains a list of all Web Services available from this Web Application. On the right side of each 'Operation' is the list of different end points that your web service is available from. Other info available includes links to example code on different ways you can call ServiceStack Web Services. As well as the XSD's and WSDL for your web services - useful if you use other tooling to examine and call your Web Services.

Clicking on the XML link for a particular Operation shows a complete example (including HTTP traffic) on how to call the selected web service as well as the response to expect. I find this useful for times when I want to check the output of a Web Service without having to load Mono Develop or VS.NET to find out.

GetDirectoryInfo XML example