This post is about how to develop a self-hosting ServiceStack app on Linux, using a
text editor, Mono, and xbuild.
Your first response to that may well be: “Oh god why?”
ServiceStack on Mono
Well, we use ServiceStack at work to develop our services. It’s great. I wanted to take a deeper dive into SS and have a play around at home. Linux is my primary OS at home, and as ServiceStack has first-class Mono support as a priority feature, I thought it could be a fun experiment.
While SS is developed to be able to run on Mono, I believe generally the dev experience is more that of “develop in Visual Studio, deploy to Linux server.” Which makes sense, as VS2012 is a pretty A+ development environment, especially for anything C# related. However, just to be deliberately perverse, and perhaps learn something in the process, I’m going to do the development in Linux with a text editor and xbuild.
MonoDevelop vs Text Editor
OK, so why not use MonoDevelop (or Xamarin Studio) though? Main reason — I love Vim. In Visual Studio I can use the rather amazing VsVim, but MonoDevelop’s Vim binding are not a patch on that, so I’ll be using the real deal here in Linux. Vim and tmux is my IDE.
Furthermore, having to manually fiddle around with the .csproj and .sln files is a useful learning experience, and something you usually end up doing when you start looking at building on a CI server anyway, so it’s all good experience. IDEs hide you from the details, which sometimes you want, but it can be good to take a look under the hood occasionally.
Also, what with ScriptCS coming down the pipeline, maybe it’s worth getting into the habit of coding C# in Vim…
Anyway, the whys and wherefores aside, here’s how to get it all set up.
Setup
Mono
First off, of course, we need Mono. On Ubuntu the following should do the trick:
1
|
|
Setup a solution
For now I’m using MonoDevelop to create the skeleton of a solution to work with.
I’m hoping to move to a bootstrap project using something like warmup
or lad shortly. For now though:
1
|
|
Once installed, within MonoDevelop create a new Console application called HelloWorld. Once that’s done, close out of MonoDevelop as henceforth we’ll be amending the solution via our text editor.
NuGet
We’re going to install ServiceStack via NuGet, and we’re going to do it via automatic package restore.
Bootstrapping NuGet
In order to do so first we’ll need to grab a copy of NuGet.exe from codeplex.
We also need to do:
1
|
|
And get a copy of Microsoft.Build.dll from a Mono for Windows installation. This blog post has more info on this, and this blog post has a copy of Microsoft.Build.dll handily available for download.
Package restore
Next we’ll set up the .nuget package restore folder in our solution, and add the NuGet.exe and NuGet.targets file to it.
Using NuGet Without Commiting Packages describes what Visual Studio does for us when we select Enable NuGet Package Restore, and we’ll recreate that here.
Make sure Microsoft.Build.dll is in the same directory as your NuGet.exe and then run:
1 2 3 4 5 6 |
|
We can add these to our solution with:
1 2 3 4 5 6 7 8 |
|
(I didn’t bother with NuGet.Config, as it just tells source control to ignore our packages folder. We can do that ourselves, say in .gitignore if we’re versioning with git.)
Next we set up the .csproj file to expect package restoration.
Add a RestorePackages tag and an import to NuGet.targets using the following:
1 2 3 4 |
|
(See this post and comment).
I also found that I needed to remove a trailing space in the RestoreCommand setting in NuGet.targets:
1 2 |
|
I also uncommented the nuget.org package source:
1 2 3 |
|
Setup packages.config
Once that’s all done, we should be hot to trot and can set up a list of the packages we want. This is done in packages.config.
1 2 3 4 5 6 7 8 9 10 |
|
OK, great. We also need to actually add the references to our project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
- use some vim-fu to speed this up
And finally… the code!
As per the ServiceStack wiki, we can get going with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Build it with xbuild:
1
|
|
Run the exe:
1
|
|
Browse to the service:
http://localhost:1337/hello/World!
Et voila!
Next steps
I’d like to look at also serving up Razor views straight from the service.
Also, as mentioned, I’m going to try and set up a bootstrappable project template with warmup/lad that avoids having to do most of the grunt-work described in this post.