-->
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.  [ 12 posts ] 
Author Message
 Post subject: newbie question: "Could not add assembly named: NHibern
PostPosted: Tue Apr 25, 2006 12:39 am 
Newbie

Joined: Tue Apr 25, 2006 12:12 am
Posts: 4
Location: Brazil
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

nhibernate-1.0.2.0

following the quick start tutorial

Hi, Pals! I'm a newbie on NHibernate, and I'm facing a basic trouble when trying to execute the Quick Start Guide (http://www.hibernate.org/362.html):

I have followed the tutorial line by line, but in the following code line an error is occurring:

Code:
Configuration cfg = new Configuration();
cfg.AddAssembly("NHibernate.Examples"); //error occurrs here!!!


The error message says: "could not add assembly named: NHibernate.Examples"

I'm trying to execute this code inside a button click, as follows:

Code:
      private void cmdOK_Click(object sender, System.EventArgs e)
      {
         Configuration cfg = new Configuration();
         cfg.AddAssembly("NHibernate.Examples.QuickStart");

         ISessionFactory factory = cfg.BuildSessionFactory();
         ISession session = factory.OpenSession();
         ITransaction transaction = session.BeginTransaction();
...


I tryed the example in VS2K3 and VS2K5, nothing changes...

Any help will be appreciated. Thanks in advance,

TTYman
ttyman@hotmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 2:37 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
Did you reference the assembly (or load it manually)?

If you could post the stacktrace of the exception, things might be easier to trace down.

regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 7:59 am 
Newbie

Joined: Tue Apr 25, 2006 12:12 am
Posts: 4
Location: Brazil
The NHibernate DLL was referenced in the References window in VS, and I have putted a "using statement" like this on class code top:

Code:
using NHibernate;
using NHibernate.Examples;


I'm not sure if I really understood your question. Please pardon me if I didn't. Sometimes things are not so obvious for a newbie. :-)

Thanks in advance,

TTYMan
ttyman@hotmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 8:23 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
When Configuration.AddAssembly() fails, it throws an exception. It, or more likely, the inner exception, will give you some idea as to why it can't load the assembly.

It could be a faulty mapping, or the inner exception might be a System.IO.FileNotFoundException, but without knowing anything about the exception that was thrown it's impossible to determine what's wrong.

Put a try / catch block around AddAssembly() and check the exception that gets thrown.

_________________
regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 10:02 am 
Newbie

Joined: Tue Apr 25, 2006 12:12 am
Posts: 4
Location: Brazil
Hi! The detailed error log follows:

Quote:
NHibernate.MappingException was unhandled
Message="Could not add assembly named: NHibernate.Examples"
Source="NHibernate"
StackTrace:
at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
at TesteNHibernate1.Form1.cmdTeste_Click(Object sender, EventArgs e) in C:\Documents and Settings\morfn\Meus documentos\Visual Studio 2005\Projects\TesteNHibernate1\TesteNHibernate1\Form1.cs:line 28
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at TesteNHibernate1.Program.Main() in C:\Documents and Settings\morfn\Meus documentos\Visual Studio 2005\Projects\TesteNHibernate1\TesteNHibernate1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


My whole window form code follows:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using NHibernate.Cfg;

namespace TesteNHibernate1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void cmdTeste_Click(object sender, EventArgs e)
        {
            try
            {
                Configuration cfg = new Configuration();
                cfg.AddAssembly("NHibernate.Examples");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
               

        }
    }
}


I'm sure this error is being caused by some basic mistake, but I can't find where...

Thank you very much!

TTYMan
ttyman@hotmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 11:13 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
Well, apparently AddAssembly() fails at Assembly.Load() which could be either due to a FileNotFoundException, FileLoadException or a BadImageFormatException. You can check which one of those it was by checking the InnerException property of the exception you caught (in the catch block, check ex.InnerException).

My money is on a FileNotFoundException.

Are you sure you've referenced the assembly in your project (it's present under references node of the project), and that it's actually called "NHibernate.Examples"?

_________________
regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 5:05 pm 
Newbie

Joined: Tue Apr 25, 2006 12:12 am
Posts: 4
Location: Brazil
Hi! Maybe you're right. Whe I tryed to exec this code:

Code:
                Configuration cfg = new Configuration();
                cfg.AddAssembly("CS");  /// error here!!!


I got the error (innermessage above):

Quote:
{"Could not load file or assembly 'CS' or one of its dependencies. O sistema não pode encontrar o arquivo especificado.":"CS"}
[System.IO.FileNotFoundException]: {"Could not load file or assembly 'CS' or one of its dependencies. O sistema não pode encontrar o arquivo especificado.":"CS"}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
InnerException: null
Message: "Could not load file or assembly 'CS' or one of its dependencies. O sistema não pode encontrar o arquivo especificado."
Source: "mscorlib"
StackTrace: " 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.Cfg.Configuration.AddAssembly(String assemblyName)"
TargetSite: {System.Reflection.Assembly nLoad(System.Reflection.AssemblyName, System.String, System.Security.Policy.Evidence, System.Reflection.Assembly, System.Threading.StackCrawlMark ByRef, Boolean, Boolean)}


In this case, which file is missing? Because in this example, "CS" is the namespace where I typed my mapped class (code above):

Code:
using System;

namespace CS
{
    public class User
    {
        private string id;
        private string userName;
        private string password;
        private string emailAddress;
        private DateTime lastLogon;

        public User() { }

        private string Id
        {
            get { return id; }
            set { id = value; }
        }

        private string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        private string Password
        {
            get { return password; }
            set { password = value; }
        }

        private string EmailAddress
        {
            get { return emailAddress; }
            set { emailAddress = value; }
        }

        private DateTime LastLogon
        {
            get { return lastLogon; }
            set { lastLogon = value; }
        }
    }
}


What Am I doing wrong???

Thanks a lot,

TTYMan
ttyman@hotmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 2:51 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
You can't map namespaces, you map assemblies (or classes)

What's the name of the assembly that contains the User class?
That's the name you should pass to AddAssembly().

In this setup, also make sure the mapping file is compiled as an embedded resource into the assembly.

_________________
regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject: problem with the walkthrough
PostPosted: Fri May 05, 2006 11:45 am 
Newbie

Joined: Fri May 05, 2006 11:33 am
Posts: 6
Location: London
Hi TTYman,

I had a similar problem and had to read a few things before finally striking on the answer. Two steps to fix this :

1. Replace the line
cfg.AddAssembly("NHibernate.Examples"); //error occurrs here!!!
with
cfg.AddXmlFile(@"the fully qualified path name of the User.hbm.xml file");

2. In your User.hbm.xml file, have a look at the class line

<class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples" table="users">

Change "NHibernate.Examples" to whatever the name of your assembly name is. Ie in visual studio, go Project->Properties. Under Common Properties->General, its the first item "Assembly Name".

Hope this helps!


Top
 Profile  
 
 Post subject: Re: problem with the walkthrough
PostPosted: Fri May 05, 2006 12:32 pm 
Newbie

Joined: Fri May 05, 2006 11:27 am
Posts: 8
Yes! It helped!
I was wondering that you reply to this post just some second before I was posting for asking some more help about this problem.
I had the same problem and I was not able to work around it!
So now was able to avoid the exception :-) and will continue with the evalutation of NHibernate!
Maybe should be very nice if the author of the "getting started" is going to modify the article for specifying better how to avoid the prblem.
Just only a more detail about the line in the xml file, where to replace the Nhibernate.Examples:

<class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples" table="users">

<class name="NHibernate.Examples.QuickStart.User, thenameoftheassembly" table="users">


Thanks again!
ema

toast wrote:

I had a similar problem and had to read a few things before finally striking on the answer. Two steps to fix this :

1. Replace the line
cfg.AddAssembly("NHibernate.Examples"); //error occurrs here!!!
with
cfg.AddXmlFile(@"the fully qualified path name of the User.hbm.xml file");

2. In your User.hbm.xml file, have a look at the class line

<class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples" table="users">

Change "NHibernate.Examples" to whatever the name of your assembly name is. Ie in visual studio, go Project->Properties. Under Common Properties->General, its the first item "Assembly Name".

Hope this helps!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 4:59 am 
Newbie

Joined: Fri May 05, 2006 11:33 am
Posts: 6
Location: London
Glad it helped, yes a bit more of a description would have helped save a lot of time, but i suppose this way, we now know how to fix these problems :).

I'm quite impressed with hibernate... coming from a J2EE background, this process is much easier than Entity Beans.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 6:41 am 
Newbie

Joined: Fri May 05, 2006 11:33 am
Posts: 6
Location: London
Hi guys,

further to this, I actually figured out what the problem was. We didn't read the Quickstart guide properly. The real important part was mentioned :

NOTE: If you are using Visual Studio .NET to compile make sure that you set the Build Action of the User.hbm.xml file to Embedded Resource. The mapping file will now be a part of the Asssembly. The subtle detail's importance will be evident later.

Ie click on the User.hbm.xml file, in the properties, set the "Build Action" to "Embedded Resource". Now the AddAssembly will work.


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