Hi
I wrote an application that load the assembly entities from other base path location, when I try to consult some entities it throw an exception.
When I trace exception and analyse the problem, understood the next scenario. The class NHibernate.Util.ReflectHelper is responsable for loads all types from assembly through TypeFromAssembly method, and in my problem, the method load assemblies from the base path load assembly, that are locate in the same path of NHibernate.dll, but my assembly locates in other path and it produce that Runtime CLR doesn't locate my assembly.
To resolve, I added some secuence logic, first I loaded the assembly in the same AppDomain.CurrentDomain and finally added the some code lines to TypeFromAssembly Method (class: NHibernate.Util.ReflectHelper, NHibernate):
public static System.Type TypeFromAssembly(AssemblyQualifiedTypeName name, bool throwOnError)
{
try
{
Assembly assembly;
// Try to get the type from an already loaded assembly
System.Type type = System.Type.GetType(name.ToString());
if (type != null)
{
return type;
}
if (name.Assembly == null)
{
// No assembly was specified for the type, so just fail
string message = "Could not load type " + name + ". Possible cause: no assembly name specified.";
log.Warn(message);
if (throwOnError) throw new TypeLoadException(message);
return null;
}
//Added by Christian
//Load current assembly registered in the Domain
//Begin Added Source Code
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assemblyLocal in assemblies)
{
if (assemblyLocal.FullName.IndexOf(name.Assembly) >= 0)
{
type = assemblyLocal.GetType(name.Type, throwOnError);
if (type != null)
{
return type;
}
else
{
log.Warn("Could not load type " + name + ".");
return null;
}
}
}
//End Added Source Code
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, throwOnError);
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);
}
if (throwOnError) throw;
return null;
}
}
I think that it is a way to resolve, but I put this on Forum to analyse the case and perhaps find another better smart solution.
What do you think to improve this case in the next versions of NHibernate?
I used Hibernate version: 1.2.1.4 .
_________________ Christian
|