Tutorial :NHibernate 1.2 in a .NET 4.0 solution



Question:

I have some projects based on NHibernate 1.2 and I'd like to add them to a .NET 4.0 solution, but I get an AmbiguousMatchException.
No matter if these projects are targeted to 2.0 or 4.0 framework.
It works if I add them to a .NET 3.5 solution.

Does anyone have experience with that?

Here is the exception:

[AmbiguousMatchException: Ambiguous match found.]     System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers) +9607924     System.Type.GetMethod(String name) +29     Castle.DynamicProxy.Builder.CodeBuilder.SimpleAST.LockBlockExpression.Emit(IEasyMember member, ILGenerator gen) +192     Castle.DynamicProxy.Builder.CodeBuilder.SimpleAST.ExpressionStatement.Emit(IEasyMember member, ILGenerator gen) +52     Castle.DynamicProxy.Builder.CodeBuilder.AbstractCodeBuilder.Generate(IEasyMember member, ILGenerator il) +370     Castle.DynamicProxy.Builder.CodeBuilder.EasyMethod.Generate() +71     Castle.DynamicProxy.Builder.CodeBuilder.AbstractEasyType.EnsureBuildersAreInAValidState() +706     Castle.DynamicProxy.Builder.CodeBuilder.AbstractEasyType.BuildType() +90     Castle.DynamicProxy.Builder.CodeGenerators.BaseCodeGenerator.CreateType() +55     Castle.DynamicProxy.Builder.CodeGenerators.ClassProxyGenerator.GenerateCode(Type baseClass, Type[] interfaces) +573     Castle.DynamicProxy.Builder.DefaultProxyBuilder.CreateClassProxy(Type theClass, Type[] interfaces) +87     Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type baseClass, Type[] interfaces, IInterceptor interceptor, Boolean checkAbstract, Object[] argumentsForConstructor) +116     NHibernate.Proxy.CastleProxyFactory.GetProxy(Object id, ISessionImplementor session) +136    [HibernateException: Creating a proxy instance failed]     NHibernate.Proxy.CastleProxyFactory.GetProxy(Object id, ISessionImplementor session) +270     NHibernate.Persister.Entity.AbstractEntityPersister.CreateProxy(Object id, ISessionImplementor session) +17     NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation) +354     NHibernate.Impl.SessionImpl.InternalLoad(Type clazz, Object id, Boolean eager, Boolean isNullable) +52     NHibernate.Type.EntityType.ResolveIdentifier(Object id, ISessionImplementor session) +37     NHibernate.Type.EntityType.ResolveIdentifier(Object id, ISessionImplementor session, Object owner) +55     NHibernate.Impl.SessionImpl.InitializeEntity(Object obj) +187     NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session) +229     NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +702     NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +62     NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +51     NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) +18     NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes) +81     NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session) +36     NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results) +315     NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria) +66     NHibernate.Impl.CriteriaImpl.List() +54       [my code calling Criteria.List()]  


Solution:1

Following Rup's comment, I resolved changing the sources of Castle DynamicProxy 1.1.5 and recompiling.
The problem is invoking with reflection the method System.Threading.Monitor.Enter without specifying arguments (that's because in .NET 2.0 there is only 1 signature), but since .NET 4.0 that method has 2 overloads.

I've modified the class Castle.DynamicProxy.Builder.CodeBuilder.SimpleAST.LockBlockExpression, changing the row

gen.Emit(OpCodes.Call, typeof(Monitor).GetMethod("Enter"));  

with the row

gen.Emit(OpCodes.Call, typeof(Monitor).GetMethod("Enter", new Type[] { typeof(object) }));  

I've got the sources of NHibernate 1.2 from SourceForge while I disassembled the code of Castle DynamicProxy 1.1.5 with Reflector.


Solution:2

NHibernate 1.x is completely unsupported at this point. You should upgrade at least to 2.1.2.

In any case, it's clear from the error message that the problem is not in NHibernate itself, but in Castle DynamicProxy.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »