-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Load assembly entities
PostPosted: Tue Dec 18, 2007 11:37 am 
Newbie

Joined: Tue Dec 18, 2007 9:03 am
Posts: 1
Location: Venezuela
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 11:51 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Since we have assemblies in various non-standard locations, we deal with all assembly loading issues by subscribing to AppDomain.AssemblyResolve. We then locate and return the assembly ourselves.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.