Fun with the Nancy Self Host! | Brendan Tompkins
If you’re not familiar with the Nancy framework for hosting HTTP services in .net, you owe it to yourself to check it out.
Nancy is a lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono. The goal of the framework is to stay out of the way as much as possible and provide a super-duper-happy-path to all interactions.
For a nice audio introduction, check out this recent episode of Hanselminutes Understanding NancyFx With Richard Cirerol.
One of the things I like best about Nancy, other than it’s super fun and easy to stand up and build HTTP services, is the Self-Host.
At work, one thing we make are some networked edge-devices for things like kiosks that are normally deployed into the field at our client sites. One of the things I find super helpful for configuration, debugging and troubleshooting, is a simple HTTP endpoint.
Imagine a problem with an edge device out in the field – like a kiosk, camera, DIO controller, etc. One of the first things a sysop may do is browse to the device’s HTTP endpoint, pull up a web page, check configuration and logging. There are certainly more sophisticated ways of going about management and troubleshooting, but for simple cases an administration web page works well. It often can save a sysop a trip out into a hazardous environment if a quick fix can be done remotely, even better using a secure browser session.
So what are your options for doing this? If you already have an existing .NET application running on your device, with state, etc. spinning up IIS and a web site isn’t a great option.. You will have to tackle issues of sharing data, installation of the web site every time you setup a new device, the overhead of running IIS, IIS security, just to name a few. What if there was a simple way of generating an HTTP endpoint? – Enter Nancy Self Hosting.
With the Nancy Self-Host, you can easily make your HTTP endpoint part of your existing application, which means you can share state, installation process, security, authentication etc. And you can do it all on the “Super Duper Happy Path.” Here’s how easy it is to host Nancy in a console app:
How about from a Service? Here’s how easy it is to host Nancy from within your own TopShelf service.
I’ve put a quick repo up on Github as a demo for hosting Nancy under TopShelf.
In short, I personally see a huge sweet spot for Nancy in these cases where you need a simple management endpoint. Next time, I’ll blog about how to do this from NServiceBus when using the NSB host – even sharing the NSB IOC container.
Happy Self Hosting!