Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Fun with the Nancy Self Host!

Nancy Logo

If you’re not familiar with the Nancy framework for hosting HTTP services in .net, you owe it to yourself to check it out.

From the docs:

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!

About Brendan Tompkins

Brendan runs CodeBetter.Com. He was twice awarded MVP for Microsoft .NET, and is a founder and the CTO of Quick180.Com More about Brendan at https://www.linkedin.com/codebetter
This entry was posted in .NET, Uncategorized and tagged , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • AquaBirdConsult

    I know the TopShelf  team is talking about bringing shelving back. I personally would like to be able to monitor services and turn them off and on remotely, without having to log in remotely. Seems like something Nancy would be perfect for.

    The original TopShelf dashboard ran on Nancy, but it was “incomplete” to say the least.

  • http://codebetter.com Brendan Tompkins

    Me too.. remember the old days of all that service ceremony?  That sucked.

  • AquaBirdConsult

    I love TopShelf. It is one of my go to projects now whenever I want to create a windows service. I created an open source wrapper for Ducksboard and I think it would be cool to hook up a TopShelf service to it.