Am playing with ASP.NET MVC these days in preparation for a couple o’ presentations on it. Jeffrey Palermo‘s CodeCampServer sample app has proven invaluable in this regard even if I have become overly cognizant that I’m ripping off his ideas.
In my own version, I’m also referencing NHibernate in my data access layer and whenever I do that, I always forget which assemblies it requires to tag along with it. In my sample, I brought over NHibernate.dll, Iesi.Collections.dll, and log4net.dll into my lib folder and added the reference to NHibernate.dll from the project. Wired up all my repositories, hooked in the IoC container (in this case, Windsor) and loaded the page:
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Not overly helpful and although my lead-up to this has made it painfully obvious, let me tell you when you’ve also added the IoC container and three other layers ‘twixt the UI and the DataAccess layer, ’tweren’t altogether clear just what was causing this problem.
In fact, even when I reverted all the code in the control back to use the simplest possible controller action, I still got this error. The reason being: I still had the reference to the DataAccess project as well as the references to its classes in the Windsor container. Which means it was still trying to load NHibernate.
And NHibernate (or at least the version of it I have) requires, along with the other assemblies mentioned above, a reference to Castle.DynamicProxy.dll. So I dropped the assembly into the lib folder and it came along for the ride the next time I compiled. Problem solved. Reminder stored.
Kyle the Proxied
*UPDATE* I’m resurrecting this bad boy at the request of one of my more moderate commenters below to reward him for his tact.
Those of you looking for some resources on using NHibernate with MVC, the best I can think of at the moment would be to review the S#arp Architecture source code and sample application. http://code.google.com/p/sharp-architecture/
Even if you can’t use the architecture itself, it is a great learning tool for how to work with NHibernate within MVC. While it uses Fluent NHibernate, there are notes on using it with plain NHibernate as well.
You could also check out Code Camp Server (http://code.google.com/p/codecampserver/) though I haven’t looked at it in a while. Also, check out the Summer of NHibernate series: http://www.summerofnhibernate.com/. Session 13 is the one on using it in web applications but I’m told it’s all gold.