Stack Overflow! MVC Model Binders Gone Awry
The recently released MVC Beta 1 has some pretty cool features. One of the features that I've been using for a while is Model Binders. These are now turned on by default which can cause some problems if you're not expecting this behavior. For example, say you have an action as follows:
public ActionResult Binder(Dog dog)
{
return View("Index");
}
Here's the model:
public class Dog
{
public string Name { get; set; }
public Dog cat { get; set; }
}
Calling this action will cause a stack overflow error, as the default model binder will attempt to recreate every dog in the chain.
I would not have noticed this as I use my own implementation of the model binders to provide functionality for pulling an entity from NHibernate that is passed by ID via Ajax. The problem is that I recently switched over to the [Transaction]/ATM method of managing transactions with NHibernate. The problem with this method is that it creates a dynamic proxy of your controller so it can intercept the transaction. In doing so it looses the method argument attributes and then - boom - stack overflow. So now I'm off to hack YET ANOTHER open source project to see if I can get it to not remove the method parameter attributes. :(

October 20th, 2008 at 6:48 am
Ah, that's a bummer. Just remember: the community appreciates your hard work! ;)
October 20th, 2008 at 8:29 am
I gave up on that idea when I found out it was a reported bug marked as "Won't Fix". I figure if the current committers figured it was too difficult to fix, I didn't need to bang my head against it for a day. I implemented it via ActionFilters in MVC and have submitted that to MvcContrib.
October 20th, 2008 at 11:37 am
This is the same problem reported at http://forums.asp.net/t/1335683.aspx. The team is deciding how best to go about fixing this issue, but that link provides some immediate workarounds to unblock you.