NHibernate ISession.Query<T> in Mono/NUnit throwing exception
I'm attempting to get my first NHibernate project up and running in Mono/openSUSE 11.4. My unit tests are working successfully under Windows but when I attempt to run the same tests on Mono I'm getting an exception when I try to get back the Query obje开发者_如何学Cct from my NHibernate session.
Saving data through NHibernate seems to be working correctly.
I'm using Mono 2.10.2 and NUnit version 2.4.8 with Fluent NHibernate 1.2 for NHibernate 3.1.
I've included the full output from nunit-console just in case:
>NUnit version 2.4.8
>Copyright (C) 2002-2007 Charlie Poole.
>Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
>Copyright (C) 2000-2002 Philip Craig.
>All Rights Reserved.
>
>Runtime Environment -
>OS Version: Unix 2.6.37.1
>CLR Version: 4.0.30319.1 ( 2.10.2 (tarball Mon Apr 18 15:12:52 UTC 2011) )
>
>.F
>Tests run: 1, Failures: 1, Not run: 0, Time: 1.563 seconds
>
>Test Case Failures:
>1) MyTests.MyTest : System.TypeInitializationException : An exception was thrown by the type initializer for NHibernate.Linq.NhRelinqQueryParser
>----> System.Reflection.ReflectionTypeLoadException : The classes in the module cannot be loaded.
>at NHibernate.Linq.NhLinqExpression.Translate (ISessionFactoryImplementor sessionFactory) [0x00000] in <filename unknown>:0
>at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators (System.String queryIdentifier, IQueryExpression queryExpression, System.String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory) [0x00000] in <filename unknown>:0
>at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators (System.String expressionStr, IQueryExpression queryExpression, System.String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) [0x00000] in <filename unknown>:0
>at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor (System.String expressionStr, IQueryExpression queryExpression, System.String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) [0x00000] in <filename unknown>:0
>at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor (System.String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) [0x00000] in <filename unknown>:0
>at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan (IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters) [0x00000] in <filename unknown>:0
>at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan (IQueryExpression queryExpression, Boolean shallow) [0x00000] in <filename unknown>:0
>at NHibernate.Impl.AbstractSessionImpl.CreateQuery (IQueryExpression queryExpression) [0x00000] in <filename unknown>:0
>at NHibernate.Linq.NhQueryProvider.PrepareQuery (System.Linq.Expressions.Expression expression, IQuery& query, NHibernate.Linq.NhLinqExpression& nhQuery) [0x00000] in <filename unknown>:0
>at NHibernate.Linq.NhQueryProvider.Execute (System.Linq.Expressions.Expression expression) [0x00000] in <filename unknown>:0
>at NHibernate.Linq.NhQueryProvider.Execute[IEnumerable`1] (System.Linq.Expressions.Expression expression) [0x00000] in <filename unknown>:0
>at Remotion.Data.Linq.QueryableBase`1[MyProject.Entities.Email].GetEnumerator () [0x00000] in <filename unknown>:0
>at System.Collections.Generic.List`1[MyProject.Entities.Email].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in /usr/src/packages/BUILD/mono-2.10.2/mcs/class/corlib/System.Collections.Generic/List.cs:126
>at System.Collections.Generic.List`1[MyProject.Entities.Email]..ctor (IEnumerable`1 collection) [0x0002f] in /usr/src/packages/BUILD/mono-2.10.2/mcs/class/corlib/System.Collections.Generic/List.cs:63
>at System.Linq.Enumerable.ToList[Email] (IEnumerable`1 source) [0x00006] in /usr/src/packages/BUILD/mono-2.10.2/mcs/class/System.Core/System.Linq/Enumerable.cs:2847
>at MyTests.MyTest () [0x0000c] in /tmp/source/MyTests/MyTest.cs:125
>at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
>at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000d5] in /usr/src/packages/BUILD/mono-2.10.2/mcs/class/corlib/System.Reflection/MonoMethod.cs:226
>--ReflectionTypeLoadException
>at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool)
>at System.Reflection.Assembly.GetTypes () [0x00000] in /usr/src/packages/BUILD/mono-2.10.2/mcs/class/corlib/System.Reflection/Assembly.cs:346
>at Remotion.Data.Linq.Parsing.Structure.ExpressionTreeParser.CreateDefaultNodeTypeProvider () [0x00000] in <filename unknown>:0
>at NHibernate.Linq.NHibernateNodeTypeProvider..ctor () [0x00000] in <filename unknown>:0
>at NHibernate.Linq.NhRelinqQueryParser..cctor () [0x00000] in <filename unknown>:0
Following AlexCuse's advice I ran monodis against the DLLs. All of the NHibernate dlls are set to CopyLocal. They seem to be happy with monodis. NHibernate.dll doesn't however. Here is the end of the output:
class NHibernate.Context.WcfStateExtension V_0) IL_0000: call Missing method get_Current in assembly
/tmp/source/MyTests/NHibernate.dll, type System.ServiceModel.OperationContext [System.ServiceModel]System.ServiceModel.OperationContext::get_Current() IL_0005: callvirt Missing method get_Extensions in assembly /tmp/source/MyTests/NHibernate.dll, type System.ServiceModel.OperationContext instance <> [System.ServiceModel]System.ServiceModel.OperationContext::get_Extensions() IL_000a: callvirt Missing method Find in assembly /tmp/source/MyTests/NHibernate.dll, type Typespec 0x1b00017e Missing method Find in assembly /tmp/source/MyTests/NHibernate.dll, type Typespec 0x1b00017e * Assertion at get.c:2097, condition `mh' not met
Aborted
mono -v
Mono JIT compiler version 2.10.2 (tarball Mon Apr 18 15:12:52 UTC 2011)
Copyright (C) 2002-2011 Novell, Inc and Contributors. www.mono-project.com
TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: amd64
Disabled: none
Misc: debugger softdebug
LLVM: yes(2.9svn-mono)
GC: Included Boehm (with typed GC and Parallel Mark)
I gave up and installed Fluent NHibernate 1.1 with NHibernate.Linq-2.1.2-GA. All seems to be happy in the world of my unit tests but I'm still trying to figure out what is going so wrong with the current version.
I rolled back to use NHibernate 2.1.2 with the additional Linq library which worked fine but then I revisited this the other day I found the fix was ridiculously simple.
The mono-wcf package was not installed on my system. NHibernate 3.2 requires WCF. Now I've installed it, it is all working perfectly!
Are all of your libraries referenced with "CopyLocal" set to true? If so, running
/path/to/mono/monodis <DLLNAME>
on your bin directory may help to determine if they are valid assemblies: (http://comments.gmane.org/gmane.comp.gnome.mono.aspnet/1106)
I've seen this (on older versions of mono AND fluent NHibernate) when dealing with classes that were not CLS-compliant, but in that case it was able to load the assembly to use other classes (I was not loading with reflection though).
精彩评论