Starting an OWIN ASP.NET server on Raspberry PI 2 automatically at boot up
Inspired by the buzz that engulfed the Microsoft community when it was announced the new Raspberry PI 2 was going to support Windows 10 I actually got myself one too, with the aid of community member Maurice Spronkers (thank bro!)
Windows 10 for devices is not yet out for release, so here I was, being a bit impatient to apply my .NET skills, looking at a device that, at the time of this writing, only can be powered by Linux. I have extensive UNIX experience, but after the GIS world largely moved to Windows NT in 1997(!) my command line skills had gotten a little rusty. Fortunately, there are this great tutorial by this awesome Belgian guy named Jan Tielens, who describes setting up the device for .NET developers and running an ASP.NET OWIN server on it.
The only thing I could not get to work, is to get the darn OWIN server to start at boot time. I found out, eventually. The problem did not so much in the configuration, as well in the default OWIN stand alone server code. Now Linux fans all over the world will probably ridicule me for this total lack of understanding of their system, let alone at how I arrived at the solution, but what the heck. Only a few learn without falling on their face first.
So after a lot of looking around of various sites, muttering on Twitter, and getting some help from various people, I arrived at this page that tells you to need edit a file named /etc/rc.local. Think of it as ye olde autoexec.bat. That, of course, you can only do as root. So you have to start that from the command line [insert muttering here]. The purists may use Nano or even VI (I still know some keystrokes if I go on automatic) but I found out that you can get a nice notepad like editor by typing
sudo leafpad /etc/rc.local
And then you add a line
su - pi -c "mono /home/pi/Desktop/OwinDemo/OwinTest.exe " &
and that should work (yeah I put my demo on the desktop. I like a GUI. Sue me). Trouble is, after rebooting, it does not. Remembering ye olde days of shell script development, I changed the line to:
su - pi -c "mono /home/pi/Desktop/OwinDemo/OwinTest.exe 2>/home/pi/Desktop/error.txt >/home/pi/Desktop/log.txt" &
Mind you, this should all be on one line! It should dump all the output of the process itself in log.txt, and possible errors in error.txt. I booted up the PI2 – nothing. But lo and behold, two files popped onto the desktop, error.txt and log.txt. So at least something did run. The first one was empty (yay, no errors!) and the second one contained just the expected text: “Press Enter to quit.” So why did I get “This page cannot be displayed”
It turns out, my friends, that what works when you start it as a user, apparently does not work when you run it it the background. Apparently, although the Console.ReadLine should stop it – it doesn’t do that.
The only thing I had to change was the .NET code.
In stead of this piece of code, that everyone writes in the OWIN examples,
static void Main(string[] args) { const string baseUrl = "http://*:5000"; using (WebApp.Start<Startup>(baseUrl)) { Console.WriteLine("Press Enter to quit."); Console.ReadKey(); } }
I wrote this very naïve piece of code
static void Main(string[] args) { const string baseUrl = "http://*:5000"; using (WebApp.Start(baseUrl)) { while(true) { Console.WriteLine("Waiting..."); Thread.Sleep(2000); } } }
And verily, verily, now every two seconds my file “log.txt” got a new line and I actually could access the web site running on my PI.
I assume I am a very big dumbass for not understanding this and my logging method is very primitive, but I could not find a decent resource telling me why I was stupid and how to solve this, so I thought I’d share this with you.
AS Jan in his original article shares most of the code I forego my trademark sample solution this time, as I expect my readers at least to be capable to delete two lines of code and replace them by four.
Final thought: if you deploy this, you might want to delete the logging redirects (as you won’t like having a very big file on you desktop just saying “Waiting…” umpteen times), and you might want to set the Thread.Sleep to a higher value so save (a minute bit of) CPU cycles.
Disclaimers: no PI2’s were harmed for this article, or being forced to reboot by the flash where it’s picture was taken with ;)