Cat.hbm.xml error Could not compile the mapping document"

871 views
Skip to first unread message

Steve Lyle

unread,
Mar 1, 2016, 5:55:02 PM3/1/16
to nhusers
This line of code:
            cfg.AddFile(@"C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\Cat.hbm.xml");

Throws this error:
            An exception of type 'NHibernate.MappingException' occurred in NHibernate.dll but was not handled in user code
            Additional information: Could not compile the mapping document: C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\Cat.hbm.xml

Why?
It is as if AddFile() can't find C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\Cat.hbm.xml
In fact, if I intentionally inject a typo in the path to the file I get the same error.
However this line of code, immediately preceding AddFile(), doesn't have any problems.
            cfg.Configure(@"C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\hibernate.cfg.xml");
I also find if I define the configuration in Web.config then there isn't any trouble.
If I don't include the path-file literal in Configure() then the hibernate.cfg.xml file will successfully be searched for and found in the bin\ folder ~ kind of as a undocumented default.
But <mapping>.hbm.xml file/s are not afforded the same bin\ folder courtesy.

Understand this is code from the "QuickStart" taken right off of the nHibernate website
and I have literally tried to many ways to get this to work that I'm resorting to you, my 4th level of support.

I understand by documentation and by construction nHibernate confguration() has about 12 different ways to load mappings.
And I'd like to believe if one works then all others will work alike. Personally I believe the "Embedded Resource" option is contrary to flexibility and therefore contrary good application management.
Sadly, the "Embedded Resource" option seems to be the only way to make nHibernate work.
And worse than all this is poor error reporting.
Shortcomings and underdevelopment all really buts enterprise adoption of nHibernate into question.
Is this thing really meant to be something - or is it only a toy?

-----
This is the mapping file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QuickStart" assembly="QuickStart">
  <class name="Cat" table="Cat">
    <!-- id name="Id"><column name="ID" sql-type="int" not-null="true" /><generator class="identity" /></id -->
    <id name="Id"><column name="CatId" sql-type="char(32)" not-null="true"/><generator class="uuid.hex" /></id>
    <property name="Name"><column name="Name" length="16" not-null="true" /></property>
    <property name="Sex" />
    <property name="Weight" />
  </class>
</hibernate-mapping>
-----

-----
This is the c# model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QuickStart
{
    public class Cat
    {
        private string id;
        private string name;
        private char sex;
        private float weight;

        public Cat(){}

        public virtual string Id { get { return id; } set { id = value; } }
        public virtual string Name { get { return name; } set { name = value; } }
        public virtual char Sex { get { return sex; } set { sex = value; } }
        public virtual float Weight { get { return weight; } set { weight = value; } }
    }
}
-----


-----
And this is the table DDL:
CREATE TABLE [dbo].[Cat](
[CatId] [char](32) NOT NULL,
[Name] [nvarchar](16) NOT NULL,
[Sex] [nchar](1) NULL,
[Weight] [real] NULL,
 CONSTRAINT [PK_Cat] PRIMARY KEY CLUSTERED 
(
[CatId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
-----

Gunnar Liljas

unread,
Mar 1, 2016, 7:36:32 PM3/1/16
to nhu...@googlegroups.com
So, what's the actual error? Just giving us the top level exception doesn't help much.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Oskar Berggren

unread,
Mar 1, 2016, 7:48:46 PM3/1/16
to nhu...@googlegroups.com
2016-03-01 15:49 GMT+00:00 Steve Lyle <ssl...@gmail.com>:
This line of code:
            cfg.AddFile(@"C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\Cat.hbm.xml");

Throws this error:
            An exception of type 'NHibernate.MappingException' occurred in NHibernate.dll but was not handled in user code

"not handled in user code" tells us that you haven't implement code to properly catch and PRINT the exception information.

 
            Additional information: Could not compile the mapping document: C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\Cat.hbm.xml

I'm not sure what produces that line, but it's not the full information that NHibernate generates.

 

Why?
It is as if AddFile() can't find C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\Cat.hbm.xml
In fact, if I intentionally inject a typo in the path to the file I get the same error.

That is just a guess, because you don't show the full error message. It could very well be "file not found" in one case and some syntax error in the mapping file in the other case. Like I wrote in the other thread you MUST look at the type and message of ALL InnerExceptions. The property Exception.InnerException is a very fundamental concept in understanding .Net expections, sadly many seem to miss this and only read the outer exception text. Generally the top-most exception will indicate what high-level operation failed, while the InnerExceptions will gradually reveal more detail on what exactly went wrong.

I've checked the NHibernate source code, and there appears to be exactly TWO places where a MappingException is thrown with the "Could not compile the mapping document"-message. In both cases, the exception is thrown using code similar to this:
            catch (Exception e)
            {
                var message = documentFileName == null
                                ? "Could not compile deserialized mapping document."
                                : "Could not compile the mapping document: " + documentFileName;
                LogAndThrow(new MappingException(message, e));   // <--- Notice here how the underlying exception is passed along.
            }

And it is the exception "e", which will show up as InnerException on the MappingException, and this will tell you in more detail what exactly went wrong.

Hint - your code should look like this:
try
{
     your configuration and mapping code goes here
}
catch (Exception ex)
{
    // ex.ToString() will recursively include the message and stacktrace from all inner exceptions.
    // Assuming your program runs from a command prompt.
    // Alternatively, put a breakpoint on the next line and explore the
    // excepton and inner exceptions with the Visual Studio exception helper.
    Console.WriteLine(ex.ToString());
    Console.ReadLine();
}



 
However this line of code, immediately preceding AddFile(), doesn't have any problems.
            cfg.Configure(@"C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\hibernate.cfg.xml");
I also find if I define the configuration in Web.config then there isn't any trouble.
If I don't include the path-file literal in Configure() then the hibernate.cfg.xml file will successfully be searched for and found in the bin\ folder ~ kind of as a undocumented default.
But <mapping>.hbm.xml file/s are not afforded the same bin\ folder courtesy.

Understand this is code from the "QuickStart" taken right off of the nHibernate website
and I have literally tried to many ways to get this to work that I'm resorting to you, my 4th level of support.

I understand by documentation and by construction nHibernate confguration() has about 12 different ways to load mappings.
And I'd like to believe if one works then all others will work alike. Personally I believe the "Embedded Resource" option is contrary to flexibility and therefore contrary good application management.
Sadly, the "Embedded Resource" option seems to be the only way to make nHibernate work.

Let's find the real problem before jumping to conclusions.


 
And worse than all this is poor error reporting.

NHibernate _generates_ a lot of debug and error information. The error _reporting_ is up to you as application developer - NHibernate can't know if you want to stick it in a log file, show a dialog, print to console, or whatever. However, NHibernate will log everything to log4net, so you can just enable that in your project to get very extensive information (see my reply in your other thread).
 

/Oskar


Steve Lyle

unread,
Mar 2, 2016, 10:37:05 AM3/2/16
to nhusers

Gunnar!  Again, I've given you everything nHibernate reports on the error.  Do you need a live, real-time, firsthand demo?
You know this.  Like anything else nHibernate code, at execution, runs into a problem, catches it, and throws a message.
In this case "NHibernate.MappingException' with "Could not compile the mapping document".  Search the code for references to those literals in context of AddFile()

What it really turned out to be.
AddFile() ~ can't find the file ~ no matter where I put it or what value I pass into AddFile(). ~ This by definition is a bug.  Thing I can't figure out is if it is because of IIS security.
This should not be true given the context of my running this in VStudio and on my local workstation AND that Configure(<file name>) does not have the same issue trying to access <file name>.

In terms of error reporting "Could not find file" would be more clear and the code of AddFile() should be quite capable of discerning "I cannot find the file" -v- "I could not compile the file [because I can't find it]".  I'll bet "could not compile" is a defacto catch all, which is never going to be of real help because there isn't any real troubleshooting depth beyond the defacto.  
Real help would be look like "I can't compile this file 'X' and here is why ....".
But this get right to the only approach, "embedded resource", being the only viable method for nHibernate to load a mapping ~ contrary to documentation.
Someone on here once said "embedded resource" is the common way"  ... well... I distinguish the difference between common practice be people perfer to do that -v- common practice because it is the only functional way to make it work.
 

You guys need some help over there?
I'll be both honest and hard on you because I expect a lot of quality because quality is a choice to apply an effort and I don't value lazy.  
That and I like to see people strive and achieve.
Now that all said I love nHibernate.  I'd love to see it achieve a much greater place as the utility it is.
And if you think I'm being harsh ... well there is why.
You should here me go off at Microsoft's EF.  THAT is complete @@@ trash and should be removed from the planet.
EF is literally hurting people & dragging down careers.

Steve Lyle

unread,
Mar 2, 2016, 10:37:52 AM3/2/16
to nhusers
Your thoughts on this.
As soon as I installed and configured log4net ~ my problem with AddFile() when away.



On Tuesday, March 1, 2016 at 5:55:02 PM UTC-5, Steve Lyle wrote:

Oskar Berggren

unread,
Mar 2, 2016, 10:57:55 AM3/2/16
to nhu...@googlegroups.com
2016-03-02 13:29 GMT+00:00 Steve Lyle <ssl...@gmail.com>:

Gunnar!  Again, I've given you everything nHibernate reports on the error.  Do you need a live, real-time, firsthand demo?
You know this.  Like anything else nHibernate code, at execution, runs into a problem, catches it, and throws a message.
In this case "NHibernate.MappingException' with "Could not compile the mapping document".  Search the code for references to those literals in context of AddFile()

Did you read my reply? Because I did just that: "searched the code for references" and as I told you the code proves there would be an InnerException that explains in further detail what the cause is.


 

What it really turned out to be.
AddFile() ~ can't find the file ~ no matter where I put it or what value I pass into AddFile(). ~ This by definition is a bug.  Thing I can't figure out is if it is because of IIS security.
This should not be true given the context of my running this in VStudio and on my local workstation AND that Configure(<file name>) does not have the same issue trying to access <file name>.

I don't remember the exact semantics, but if you give a full absolute (and correct) path to AddFile() and it still can't read the file, then yeah something with file access permissions could be involved.

 
In terms of error reporting "Could not find file" would be more clear and the code of AddFile() should be quite capable of discerning "I cannot find the file" -v- "I could not compile the file [because I can't find it]".  I'll bet "could not compile" is a defacto catch all, which is never going to be of real help because there isn't any real troubleshooting depth beyond the defacto.  

In my other reply I've already shown you the NHibernate code that throws the exception. If you had used ToString() on the exception (which includes InnerException), you would have gotten a message similar to:
"Could not compile mapping document "...."  -> File not found"
Where the latter part is the detailed information coming from the inner exception.


/Oskar


Oskar Berggren

unread,
Mar 2, 2016, 11:00:19 AM3/2/16
to nhu...@googlegroups.com
2016-03-02 15:14 GMT+00:00 Steve Lyle <ssl...@gmail.com>:
Your thoughts on this.
As soon as I installed and configured log4net ~ my problem with AddFile() when away.


It's very likely to be just coincidence, but we will never know because we never got to see the text from the InnerException so we can only guess.


/Oskar




Gunnar Liljas

unread,
Mar 2, 2016, 12:25:59 PM3/2/16
to nhu...@googlegroups.com
Just verified. If you call AddFile with a file that doesn't exist (according to the environment), this is the actual exception you get, and you would have seen it if you had just looked for it.

NHibernate.MappingException : Could not compile the mapping document: test.xml
  ----> System.IO.FileNotFoundException : Could not find file 'c:\......\test.xml'.

It is NOT a bug. AddFile is a perfectly viable method to use. Of course it requires that the file actually exists.

"You guys need some help over there?"

Always.

"I'll be both honest and hard on you because I expect a lot of quality because quality is a choice to apply an effort and I don't value lazy."

1. NHibernate is an open source project handled on spare time. You can have expectations but you can't require those expectations to be met. 
2. Lazy? Really?

"And if you think I'm being harsh"

I think your attitude and vocabulary reduces your chance of getting help, especially when you don't listen to the advice we've been trying to give. Instead of saying "it's a bug!", "contrary good application management" or "Shortcomings and underdevelopment ", you could have just said "Sorry, I don't know how to access the real error message. Please show me how. In code.". 

/G


--

Steve Lyle

unread,
Mar 2, 2016, 12:47:21 PM3/2/16
to nhusers
Turns out I misspoke.
Where I did change the mapping from "Embedded Resource" back to "Content" Microsoft VStudio must still be Embedding the mapping as a resource.
I find this out because I added another mapping, rebuilt, and the old error came back "hibernate:Cannot compile".  And I thought it odd that log4net didn't log it at all.  But log4net did log that it loaded the the preexisting mappings as embedded resource.

OR maybe I make this stuff up.

Steve Lyle

unread,
Mar 2, 2016, 2:58:58 PM3/2/16
to nhusers
I respect your ability to comment on my attitude.
I will agree-to-disagree in AddFile().  I don't think something is viable when it is said to work but in all reality doesn't no matter how it's use is attempted.
Perhaps it works in older implementations (VS2010 & .Net 2 & Web Forms) or maybe other platforms.
But in my case it is said to work ~ but it just plane doesn't.
Most people are using Embedded Resource ~ most likely for a reason.

But I'm done.  I can chose we use hbm - or something else.
The enormity of my and other's man-hours I'm not sure where I'll go with it.

Thanks for you help and looking into this issue with me.
Respectfully.

Steve Lyle

unread,
Mar 2, 2016, 2:59:13 PM3/2/16
to nhusers
And yet I can cut and past the string literal from between the "(" & ")" and paste it into start/run and the file opens in a browser.
And this line immediately preceding AddFile() has no issues openeing & using hibernate.cfg.xml
Configuration cfg = new Configuration(@"C:\Users\slyle\Documents\Visual Studio 2015\Projects\Cat\Cat\Models\hibernate.cfg.xml").Configure()


It doesn't matter.
I'm done.
Not worth it.

Thanks for the feedback.



On Tuesday, March 1, 2016 at 5:55:02 PM UTC-5, Steve Lyle wrote:

Gunnar Liljas

unread,
Mar 2, 2016, 3:29:39 PM3/2/16
to nhu...@googlegroups.com
"I will agree-to-disagree in AddFile().  I don't think something is viable when it is said to work but in all reality doesn't no matter how it's use is attempted."

Could you at least give us the courtesy to investigate the exception thrown? If there is a bug, we'd like to know that, but most likely something else happens and the exception will give you the info you need. Look at the InnerException!

"Most people are using Embedded Resource ~ most likely for a reason."

Yes, it's because it embeds the file, making it impossible to tamper with from the outside and also it simplifies deployment.

/G

Carlos Cubas

unread,
Mar 2, 2016, 3:59:10 PM3/2/16
to nhu...@googlegroups.com
Gunnar, you've shown incredible patience here.  Don't waste your time.

Steve Lyle

unread,
Mar 2, 2016, 10:42:09 PM3/2/16
to nhusers
I offer to live, real-time, firsthand demo.
But I have a stable project now and have spend much to much time on this.
I'm not going to undo what I have to re-create this error.
When time permits I'll see if I can create a new project to demo to you.
But please understand I've given to you all that the catch(exception ex) {}  Exception object has to report.  I've withheld nothing from you.

Gunnar Liljas

unread,
Mar 3, 2016, 1:24:49 AM3/3/16
to nhu...@googlegroups.com

Steve Lyle

unread,
Mar 3, 2016, 12:25:01 PM3/3/16
to nhusers
In my case e.InnerException == null everytime.


On Tuesday, March 1, 2016 at 5:55:02 PM UTC-5, Steve Lyle wrote:

Steve Lyle

unread,
Mar 3, 2016, 12:28:18 PM3/3/16
to nhusers
A) What error do you get when there is a typo in the XML mapping file's class-tab name-attribute value and you are using AddFile()?
   In other words when the name-attribute value != the name of the class in the class.cs file.
B) Without that typo, what error do you get when there is a typo in the string passed into AddFile()?
C) What if the typo is in the value of the assembly-attribute or the hibernate-mapping-tag and all else is without typos?
D) What if the typo is in the value of the namespace-attribute or the hibernate-mapping-tag and all else is without typos?
E) How much of log4net helped you to deal with those errors?

I don't think this is what I had going on.  Again I've gone through far to many iterations of this for me to say that at times perhaps it wasn't.
But I did just find I can make a very easy demo of this.

Try it yourself.
VStudio 2015, new project / Web api / SPA.
Add in nHibernate 4.0 & log4net 1.2.15
Add config sections & configs to web.config.

-- here is my openSessoin
        public static ISession OpenSession()
        {
            var cfg = new Configuration();
            cfg.Configure();  // nHibernate config is setup in Web.Config
            // AddFile() doesn't work.  I don't care what they say.  Everyone is setting the propertry of the XML mapping file to "Embedded Resource" for a reason.
            cfg.AddFile(@"C:\Users\slyle\Desktop\Steves NHibernate Playground\StevesnHibernatePlayground\MyFirstNHibernateSite\MyFirstNHibernateSite\Models\NHibernate\EmployeeList.hbm.xml");
            //cfg.AddFile(@"C:\Users\slyle\Desktop\Steves NHibernate Playground\MyFirstNHibernateSite\MyFirstNHibernateSite\Models\NHibernate\vEmployeeList.hbm.xml");
            //cfg.AddFile(@"C:\Users\slyle\Desktop\Steves NHibernate Playground\MyFirstNHibernateSite\MyFirstNHibernateSite\Models\NHibernate\spHireEveryone.hbm.xml");
            ISessionFactory ses = cfg.BuildSessionFactory();
            return ses.OpenSession();
        }
---

I can make the error come and go as I please at run time by simply editing EmployeeList.hbm.xml; EmployeeList.hbm.xml is not an embedded resource.
What errors do you get in all cases above?
How much does log4net help you in this case?








On Tuesday, March 1, 2016 at 5:55:02 PM UTC-5, Steve Lyle wrote:

Oskar Berggren

unread,
Mar 3, 2016, 1:27:28 PM3/3/16
to nhu...@googlegroups.com
2016-03-03 4:25 GMT+00:00 Steve Lyle <ssl...@gmail.com>:
A) What error do you get when there is a typo in the XML mapping file's class-tab name-attribute value and you are using AddFile()?
   In other words when the name-attribute value != the name of the class in the class.cs file.

log.txt:
2016-03-03 18:57:46,594 [9] ERROR NHibernate.Cfg.Configuration - Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml
NHibernate.MappingException: Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml ---> NHibernate.MappingException: persistent class NHStart.CatTYPO, NHStart not found ---> System.TypeLoadException: Could not load type 'NHStart.CatTYPO' from assembly 'NHStart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.


Console.Error.WriteLine(ex.ToString());
NHibernate.MappingException: Could not compile the mapping document: C:\Users\os
kar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml
---> NHibernate.MappingException: persistent class NHStart.CatTYPO, NHStart not
found ---> System.TypeLoadException: Could not load type 'NHStart.CatTYPO' from
assembly 'NHStart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

 

B) Without that typo, what error do you get when there is a typo in the string passed into AddFile()?

log.txt:
2016-03-03 19:02:54,388 [10] ERROR NHibernate.Cfg.Configuration - Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\CatTYPO.hbm.xml
NHibernate.MappingException: Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\CatTYPO.hbm.xml ---> System.IO.FileNotFoundException: Could not find file 'C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\CatTYPO.hbm.xml'.

Console.Error.WriteLine(ex.ToString());
NHibernate.MappingException: Could not compile the mapping document: C:\Users\os
kar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\CatTYPO.hbm.
xml ---> System.IO.FileNotFoundException: Could not find file 'C:\Users\oskar.be
rggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\CatTYPO.hbm.xml'.


 
C) What if the typo is in the value of the assembly-attribute or the hibernate-mapping-tag and all else is without typos?

log.txt:
2016-03-03 19:06:03,631 [10] ERROR NHibernate.Cfg.Configuration - Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml
NHibernate.MappingException: Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml ---> NHibernate.MappingException: persistent class NHStart.Cat, NHStartTYPO not found ---> System.IO.FileNotFoundException: Could not load file or assembly 'NHStartTYPO' or one of its dependencies. The system cannot find the file specified.

Console.Error.WriteLine(ex.ToString());
NHibernate.MappingException: Could not compile the mapping document: C:\Users\os
kar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml
---> NHibernate.MappingException: persistent class NHStart.Cat, NHStartTYPO not
found ---> System.IO.FileNotFoundException: Could not load file or assembly 'NHS
tartTYPO' or one of its dependencies. The system cannot find the file specified.


 
D) What if the typo is in the value of the namespace-attribute or the hibernate-mapping-tag and all else is without typos?

log.txt:
2016-03-03 19:08:29,147 [9] ERROR NHibernate.Cfg.Configuration - Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml
NHibernate.MappingException: Could not compile the mapping document: C:\Users\oskar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml ---> NHibernate.MappingException: persistent class NHStartTYPO.Cat, NHStart not found ---> System.TypeLoadException: Could not load type 'NHStartTYPO.Cat' from assembly 'NHStart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.


Console.Error.WriteLine(ex.ToString());
NHibernate.MappingException: Could not compile the mapping document: C:\Users\os
kar.berggren\Documents\Projects\nhibernate-core\Foo\NHStart\NHStart\Cat.hbm.xml
---> NHibernate.MappingException: persistent class NHStartTYPO.Cat, NHStart not
found ---> System.TypeLoadException: Could not load type 'NHStartTYPO.Cat' from
assembly 'NHStart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.



 
E) How much of log4net helped you to deal with those errors?

Tremendously, but just catching and analyzing the exception in my own application is also useful.



 

I don't think this is what I had going on.  Again I've gone through far to many iterations of this for me to say that at times perhaps it wasn't.
But I did just find I can make a very easy demo of this.

Try it yourself.
VStudio 2015, new project / Web api / SPA.
Add in nHibernate 4.0 & log4net 1.2.15
Add config sections & configs to web.config.

-- here is my openSessoin
        public static ISession OpenSession()
        {
            var cfg = new Configuration();
            cfg.Configure();  // nHibernate config is setup in Web.Config
            // AddFile() doesn't work.  I don't care what they say.  Everyone is setting the propertry of the XML mapping file to "Embedded Resource" for a reason.
            cfg.AddFile(@"C:\Users\slyle\Desktop\Steves NHibernate Playground\StevesnHibernatePlayground\MyFirstNHibernateSite\MyFirstNHibernateSite\Models\NHibernate\EmployeeList.hbm.xml");
            //cfg.AddFile(@"C:\Users\slyle\Desktop\Steves NHibernate Playground\MyFirstNHibernateSite\MyFirstNHibernateSite\Models\NHibernate\vEmployeeList.hbm.xml");
            //cfg.AddFile(@"C:\Users\slyle\Desktop\Steves NHibernate Playground\MyFirstNHibernateSite\MyFirstNHibernateSite\Models\NHibernate\spHireEveryone.hbm.xml");
            ISessionFactory ses = cfg.BuildSessionFactory();
            return ses.OpenSession();
        }
---

I can make the error come and go as I please at run time by simply editing EmployeeList.hbm.xml; EmployeeList.hbm.xml is not an embedded resource.
What errors do you get in all cases above?
How much does log4net help you in this case?


I had VS2010 easily at hand, and I made my sample projects simple Console Applications instead of web projects, but if you can "prove" that any of those changes make a substantial difference to the exception message in these cases you will indeed have found a very big compatibility issue (in .Net itself, not in NHibernate).

Sometimes it can be a bit tricky to get log4net working in web projects, due to difficulties getting the path to the log file correct (absolute physical path is easiest for simple tests). But you haven't complained about missing log files, so I assume you do get the file.


I'm attaching five different versions of my sample project - one is working, the other four contains each of the typos you mention. Each of them have a log-file in the bin-directory that already contains the full output I've got.

BEWARE that due to absolute paths the path to the mapping file will of course be wrong in all five projects on your machine.


 /Oskar



--

Steve Lyle

unread,
Mar 3, 2016, 8:02:21 PM3/3/16
to nhusers
DUDE! You the man :-)
So for you to provide what you did (which btw confirmed so much of what I when through) was amazing.

You see you had the experience of setting up log4net in web application for the benefit of nHibernate.
I started down this experience without that experience to draw on and where I was time time and again Exception = "hibernate.dll [cannot compile mapping file]" & InnerExecption = null.
That was it.  Nothing more.
And when I tried to work with log4net it didn't work or provide any additional information.

So then what for me?  I was in a rock and a hard place.  "Please show the InnerException?" The innerException is NULL period.
Please use log4net.  Log4net sucks too because it doesn't work!  Darn it!!!

I only wish I could Ctrl-Z undo all the way back out to the version of the code that started me off on this google group days ago.
But only as of this morning I have log4net working now.  And in a couple of instances I can replicate the samples from your error log files.

I'll assume I had multiple typos in file-path &/ namespace &/ class &/ assembly ~ like a perfect storm ~ and the only error message I could achieve was "can't compile mapping" - PLEASE!

I don't know what the challenge is but I would like to think the top level exception message could be:
   "can't find your file (probably because of a typo in ...)" fix that then
   "can't find the assembly (probably because of a typo in ...)" fix that then
   "can't find the namespace (probably because of a typo in ...)"  and so on.
Why can't the most important and probably always the most frequent and relevant error be the first dataum presented, the outer exception message?
Maybe it is a java thing but I rarely need to go into innerexceptions anymore.





On Tuesday, March 1, 2016 at 5:55:02 PM UTC-5, Steve Lyle wrote:

Oskar Berggren

unread,
Mar 4, 2016, 5:40:31 AM3/4/16
to nhu...@googlegroups.com
2016-03-03 20:55 GMT+00:00 Steve Lyle <ssl...@gmail.com>:
 
I don't know what the challenge is but I would like to think the top level exception message could be:
   "can't find your file (probably because of a typo in ...)" fix that then
   "can't find the assembly (probably because of a typo in ...)" fix that then
   "can't find the namespace (probably because of a typo in ...)"  and so on.
Why can't the most important and probably always the most frequent and relevant error be the first dataum presented, the outer exception message?
Maybe it is a java thing but I rarely need to go into innerexceptions anymore.


.Net is structured around "nested" exceptions. The idea is that the inner most code can encounter something like "file not found" but that exception in itself does NOT help much because in a large system there could be hundreds of places where such an exception could occur. The idea is that a slightly higher level of the application can add an outer exception with more context information. Then an even higher level can add yet another outer exception with even higher level contextutal information to describe during what high level task the low level problem occurred.

This is the way .Net is designed and we're not going to work against that in NHibernate because it would be to go against the guidelines. I do wish Microsoft would have made this thing more prominent in the documentation  - there is FAR too much code out there that just grabs ex.Message (and sometime ex.Stacktrace), completely ignoring the chain of inner exceptions. Stackoverflow is full of questions that would never have needed to be asked if the poster had just known about or bothered to check the inner exception first. There really should have been a readonly ex.MessageFull or something to make you stop and think even if you're just using VS Intellisense to guide you.

As for why/how you get null in the InnerException I don't think I will spend much time to guess. One thing to beware of is that if the code in your catch-handler does something wrong, it will throw a new exception, thereby completely hiding the existence of the first exceptions. When this happens it can be very baffling and tricky to figure out.


/Oskar


 

--
Reply all
Reply to author
Forward
0 new messages