I started using NHibernate with Visual Web Developer Express and had some troubles to get it to work. The main problem is that we don't know the names that will be generated for the assemblies, as they are random. To solve this I do not define the assembly name in the mapping files and made a minor change to the ReflectHelper to search for the type definition in all loaded assemblies. I would like that any NHibernate team member evaluate if this change could be incorporated to the releases of HNibernate. The change was done in TypeFromAssembly method:
Code:
public static System.Type TypeFromAssembly( AssemblyQualifiedTypeName name )
{
System.Type type = null;
try
{
/* original code
// Try to get the type from an already loaded assembly
System.Type type = System.Type.GetType( name.ToString() );
if( type != null )
{
return type;
}
*/
////////
/// Search type in all loaded assemblies
AppDomain currentDomain = AppDomain.CurrentDomain;
Assembly[] loadedAssemblies = currentDomain.GetAssemblies();
for ( int i = 0; i < loadedAssemblies.Length; i++ )
{
AssemblyQualifiedTypeName auxName = new AssemblyQualifiedTypeName( name.Type, loadedAssemblies[i].FullName );
type = System.Type.GetType( auxName.ToString() );
if ( log.IsDebugEnabled )
{
log.Debug(" Trying to load " + auxName.ToString() + " from " + loadedAssemblies[i].FullName );
}
if ( type != null ) return type;
}
////////
if( name.Assembly == null )
{
// No assembly was specified for the type, so just fail
log.Warn( "Could not load type " + name + ". Possible cause: no assembly name specified." );
return null;
}
Assembly assembly = Assembly.Load( name.Assembly );
if( assembly == null )
{
log.Warn( "Could not load type " + name + ". Possible cause: incorrect assembly name specified." );
return null;
}
type = assembly.GetType( name.Type );
if( type == null )
{
log.Warn( "Could not load type " + name + "." );
return null;
}
return type;
}
catch( Exception e )
{
if( log.IsErrorEnabled )
{
log.Error( "Could not load type " + name + ".", e );
}
return null;
}
}