Wednesday, 2 July 2014

A Quick Hack Into Nancy

Nancy, a very neat way to build HTTP based services on .NET and mono, has been on the market for quite a while (I suppose?). Since I work in a very interesting environment, I have not actually got a chance to take a look at the solution, until then.




As far as I understand, Nancy is the next generation web application framework in .NET, after Web Forms, MVC and WebAPI. Even more, Nancy provides a slick way to develop RESTful web services. Since Nancy's documentation is pretty comprehensive, I will just crack on the hack.

As usual, visual studio is the place we go. Since I am using Visual Studio 2013, there is a slight change to the way web application frameworks. There is only two selections left.

The Web Forms, MVC and WebAPI frameworks are now in a sub category in Web Application (perhaps Microsoft has got some evil plans?). But we just need an empty web project to kick off.

Here is all we have and need, minimum dlls and a web.config.

Then we basically need to choose how we want to host the application, and choose a corresponding package. And this is another reason why I think Nancy is really cool, it offers you to be able to host four different projects at the moment, ASP.NET, self host, WCF and Owin. To start with, I would just demo with ASP.NET. Use the nuget command to get the two essential packages.

Install-Package Nancy
Install-Package Nancy.Hosting.Aspnet

And then you will find some smurfs have already help you populate the web.config.

Bear in mind, you will get into trouble if you don't have the hosting handlers declared in the config.

Following the setup, here comes the mirror and smoke.


IMPORTANT:

There is a trap that can easily lure self-declared smartass like me in. DO NOT NAME YOUR PROJECT USING THE NAME 'NANCY'. Because after all the effort you've put it, what you would get is this message of discontent: 

Could not load type 'Nancy.Bootstrapper.INancyBootstrapper' from assembly 'Nancy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null


That is definitely not what you want after joyfully try out some so-call easy peasy framework, and it makes absolutely no sense whatsoever at all. Reason is simple, JIT will confuse the type you try to load is originated from the project you created, and of course it will never find anything!

Since some people (like me) just have to learn it from the hard way, and googling is not much help in this case, I hope I can get someone out of his misery at least.

No comments:

Post a Comment