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.  [ 7 posts ] 
Author Message
 Post subject: Loading assembly mappings through reflection
PostPosted: Thu Dec 18, 2008 8:12 am 
Newbie

Joined: Wed Oct 01, 2008 10:59 am
Posts: 6
Hi,

NHibernate version: 1.2.1

I am currently writing a simple WinForms tool to export mappings to DDL using SchemaExport. It allows the user to select one or more dll files to use.

The following is done:

Code:
var asm = Assembly.LoadFile(fileName);
cfg.AddAssembly(asm);


AddAssembly throws the following (inner) exception:

Could not load file or assembly '<correct assembly name>' or one of its dependencies. The system cannot find the file specified.":"<assembly filename minus extension>"

So to me it seems that for some reason NHibernate is trying to load the assembly file without the extension.

The inner IOException is thown in NHibernate.Cfg.HbmBinder.ClassForFullNameChecked(String fullName, String errorMessage)

Any idea what I'm doing wrong here?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 8:03 pm 
Beginner
Beginner

Joined: Mon Feb 04, 2008 7:36 pm
Posts: 31
I ended up using the AddDocument(doc, name) method from the NHibernate.Cfg.Configuration class;

Load your assembly and then call
Stream stream = new MemoryStream();
stream = HbmSerializer.Default.Serialize(assembly);

This dumps the xml into the stream which you then load into the configuration.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 3:16 am 
Newbie

Joined: Wed Oct 01, 2008 10:59 am
Posts: 6
Hi,

My assembly already contains the mapping files as embedded resources. Hence the use of #AddAssembly which is supposed to add all of an assembly´s embedded resources whose names end with .hbm.xml.

I take it from your code you are having NHibernate generate a mapping based on attributes set on classes in the assembly. Correct me if I´m wrong.

I could go and try to manually extract the resources from the assembly and pass those to AddDocument, but I am not sure that would resolve this issue.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 5:54 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have you tried: cfg.AddAssembly(fileName); ? Do you get the same error here ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 10:30 am 
Newbie

Joined: Wed Oct 01, 2008 10:59 am
Posts: 6
wolli wrote:
Have you tried: cfg.AddAssembly(fileName); ? Do you get the same error here ?


I have tried AddAssembly(string) with the simple assembly name:

Code:
#AddAssembly(asm.GetName().Name);


FNFException loading file equal to asm.GetName().Name

Code:
#AddAssembly(asm.FulName);


FNFException loading file equal to asm.FullName

Ok, indeed it looks like trying the correct filename might be worth trying even if it isn't supported by the API:

Code:
#AddAssembly(asm.Location);


Alas, this results in a FileLoadException. Inner stack trace:
" at System.Reflection.AssemblyName.nInit(Assembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent)\r\n at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)\r\n at System.Reflection.Assembly.Load(String assemblyString)\r\n at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)"

Original inner stack trace for comparison:
" at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)\r\n at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)\r\n at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)\r\n at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)\r\n at System.Reflection.Assembly.Load(String assemblyString)\r\n at NHibernate.Util.ReflectHelper.TypeFromAssembly(AssemblyQualifiedTypeName name, Boolean throwOnError)\r\n at NHibernate.Util.ReflectHelper.ClassForName(String name)\r\n at NHibernate.Cfg.XmlHbmBinding.Binder.ClassForFullNameChecked(String fullName, String errorMessage)"

Vexing...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 4:49 am 
Newbie

Joined: Wed Oct 01, 2008 10:59 am
Posts: 6
Same problem using NHibernate 2.0

Anyone at all have experience with run time loading of assemblies and mapping files embedded therein?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 12:46 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We load the assembly ourselves and then loop through the embedded resources. This makes it clear when a particular resource has a problem:

Code:
foreach (string resourceName in assembly.GetManifestResourceNames())
{
    if (resourceName.EndsWith(".hbm.xml"))
    {
        try
        {
            configuration.AddResource(resourceName, assembly);
        }
        catch (Exception x)
        {
            throw new Exception(
                string.Format(
                    "Failed to add resource '{0}' from assembly '{1}' to NHibernate configuration.",
                    resourceName,
                    assembly.GetName().Name),
                x);
        }
    }
}


We also subscribe to AppDomain.CurrentDomain.AssemblyResolve, you might need to do that if NHibernate wants to load the assembly itself and has problems locating it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.