MonoTouch ServiceStack

MonoTouch tutorial Step 4 - Creating a new Web Service

Adding the GetTextFile Web Service

In keeping with the theme of sending RemoteInfo to the client, I think another simple and useful new Web Service could be to retrieve the contents of a Text file.

To do this, I've added a new class called GetTextFile in the RemoteInfo.ServiceModel.Operations namespace. This class contains the Request and Response DTO's used for this Web Service as seen below:

GetTextFile Request and Response DTO's

Even simpler than the last Web Service, it just accepts a single AtPath argument while only returning the Contents, CreatedDate and LastModifiedDate of the Text File located at AtPath.

To add the implementation for this Web Service just add a class named GetTextFileHandler at the RemoteInfo.ServiceInterface namespace with the class below:

GetTextFile Handler

A simple implementation that just checks if the argument AtPath points to a Text file, if it does it will return the Contents of the Text file otherwise it will return an empty DTO.

That's actually all there is to creating a new Web Service, just by adding the 2 files above it's now discoverable on the Web Services splash page:

And callable via REST, you can use the Web Service to view the contents of itself with the following url:
http://localhost:8080/Public/Xml/SyncReply/GetTextFile?AtPath=/Server/RemoteInfo.ServiceInterface/GetTextFileHandler.cs

Now that we've made sure the new Web Service is working, let's add it to the MonoTouch application.
To start off we'll uncomment the lines to allow the User to navigate further when it is a text file.

We'll now need to create a new View Controller to view this information. Do this by

  1. Create a new 'View Interface Definition with Controller' called ViewTextFileController
    create new controller
  2. Double-click ViewTextFileController.xib to load Interface Builder.
  3. Drag a UITextView on to the View to add it
    Add UITextView

Now in the Inspector, check to make sure you have the text ViewTextFileController in the class field of the new View Controller.
If you got this far you should have a new View Controller that looks like this:

In order to access the new TextView you will need to declare and hook up an outlet.

Do this by finding your ViewTextFileController in the Library and adding a textView outlet of type UITextView
After you've done this you can now go back to the Inspector and 'hook up' your textView by dragging the outlet and pointing it at your textView as below:

Now that we've got the visuals sorted out, we just need to add the C# class for the controller. For this we want to create a new class called ViewTextFileController with the contents below:

ViewTextFileController c# controller

This is a standard C# UIViewController class with an added constructor that takes 2 arguments: a fileName and the fileContents. You can set the Title in the constructor however we will only be able to interact with the textView after it has been loaded, i.e. in the method void ViewDidLoad()

Note: the iPhone Obj-C UIKit controls sometimes throw an error if you set it to null, in these cases you will want to set it to an empty string instead.

At this point we should have a functional ViewTextFileController all that's left is to load it, for this we'll go back to our TableViewDelegate class and add some code to load the controller when the user selects a Text file:

Our new implementation calls the GetTextFile web service passing in the full path of the Text file the user has selected. Once we receive the response we load a new instance of the ViewTextFileController with the name and contents of the Text file.

Ok that's all we need to do, time for another test run. Hit Debug and navigate to a Text file that's on the server to test it out. With this we should now be able to see the source code of the GetTextFileHandler.cs Web Service we've just created:

 

Last words

Hopefully this tutorial will help get you started in accessing your own Web Services in MonoTouch.
With just two C# source files to create a new Web Service and requiring only two lines of code to call it, calling Service Stack Web Services in MonoTouch has never been easier!

The full source code is available at: http://servicestack.googlecode.com/files/RemoteInfo.zip.

If you run into any problems with Service Stack feel free to drop me an email at demis.bellot at gmail.com.
For other useful resources, check out the MonoTouch forums or post an email to the MonoTouch mailing list at: monotouch at lists.ximian.com