System.BadImageFormatException using NHibernate proxies

by timvasil 4/24/2008 11:36:00 AM

I made a bunch of changes to code recently and then ran a unit test to find this:

NHibernate.HibernateException: Creating a proxy instance failed --->  System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->  System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B).

This happened when I tried to serialize an object graph using WCF and my handy NHibernate data contract surrogate. Strangely, if I set breakpoints in certain places the exception would not occur but proxies elsewhere would behave strangely, e.g. lose property values.

Poking around on the Castle DynamicProxy forums, there were mentions of this being due to an abstract class being proxied.  I turned off lazy initialization of classes that had abstract bases, and yet the problem persisted.  Then, on a clue that DynamicProxy lacked support for proxying generic classes, I removed the "virtual" keyword from what looked like an innocuous method:

        protected virtual IEnumerable<T> GetSubset<T>()
        {
            . . .
        }

Simply removing the "virtual" keyword fixed the problem.  So be warned:  no generic methods on your proxy classes, at least not until DynamicCastle 2.0!

Tim

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Hibernate | Castle.DynamicProxy

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments