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!

NHibernate and ASP.NET MVC

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.

This entry was posted in ASP.NET MVC, NHibernate. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://codebetter.com/blogs/kyle.baley/archive/2008/01/16/nhibernate-and-asp-net-mvc.aspx Susanta Samanta

    simly good…

  • onlyice

    Thanks for putting the link to http://code.google.com/p/sharp-architecture/ I never heard of it… thanks…

  • Pikesville Paesano

    While not liking the tone, I take Srihari point, and enjoyed your rebuttal, but since you did come up as page one on my googling of ‘mvc nhibernate’, if you added some links to resouces here, you’d be able to leverage your prominent placement in the search.

  • Kyle Baley

    Oy, angry, angry people out here in internet-land. But since you said please, I’ll fire the marketing firm I use to name my posts.

  • Srihari Kothapalli

    Useless article.. Please do not try to mislead and waste browsers time with such title.

    “NHibernate and ASP.NET MVC”

    I would have expected to see some introduction on both of these etc etc

  • http://www.jameskovacs.com James Kovacs

    When you get TypeLoadExceptions, it’s time to pull to FusLogVw.exe, which ships with the .NET Framework SDK. It will tell you exactly which assembly load is failing and why. I should probably write a blog post about it sometime… Added to my increasingly long To-Do List. :)

  • http://www.jeffreypalermo.com Jeffrey Palermo

    I also have an ASP.NET MVC sample app that uses NHibernate to get at Northwind.