But it IS an embedded resource and sometimes I build it Twice!!

12 views
Skip to first unread message

tqwhite

unread,
Mar 4, 2010, 5:06:05 PM3/4/10
to nhusers
I've spent the last two days trying to get my first bit of nHibernate
to work. I am in "No Persister" hell.

Following is everything I have relative to this problem (what I've
tried, environment, file names, xml, classes, error text).

Please, for the love of all that is holy, help me!!

tqii

What I've tried:

I've moved the file around everywhere that makes sense. It is
presently in the same directory as the class.
I took out the class definition. Nothing changes at all.
I inserted stupid assembly and namespace definitions. Nothing changes
at all.
I recreated my starter project a few times. Doesn't matter.
All of the alternatives to "embedded resource" that seemed relevant.
It is presently embedded.
I changed the code to refer to a new class with a different name but
left the hbm unchanged. Same error, different name.

Environment:

Windows 7
ASP.NET MVC
ASP.NET 3.5 sp1
Visual Studio 2008

File names:

In the same directory
studentBase.cs
studentBase.hbm.xml

Also, hibernate.cfg.xml is in the top directory of the project. I used
the sample with no changes except the connection string. I think this
is being found properly because I can induce errors by changing it.

StudentBase.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ExpressBook" namespace="ExpressBook">
<class name="ExpressBook.Services.BaseDataObjects.StudentBase,
ExpressBook">
<id name="id" column="id" type="int32" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="first" column="first" type="text" not-null="false"/
>
<property name="last" column="last" type="text" not-null="false"/>
</class>

</hibernate-mapping>

StudentBase.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ExpressBook.Services.BaseDataObjects
{
public class StudentBase
{
public virtual int id { get; set; }
public virtual string first { get; set; }
public virtual string last { get; set; }
}
}

The Offending Code:

Line 20: NHibernate.ISession session =
sessionFactory.OpenSession();
Line 21:
Line 22: return
session.Get<Services.BaseDataObjects.StudentBase>(id);

The Error Message:

Stack Trace:


[MappingException: No persister for:
ExpressBook.Services.BaseDataObjects.StudentBase]
NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String
entityName) +103
NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent
event, LoadType loadType) +112
NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType
loadType) +159
NHibernate.Impl.SessionImpl.Get(String entityName, Object id) +186
NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id) +137
NHibernate.Impl.SessionImpl.Get(Object id) +175
ExpressBook.Models.Student.getStudentById(Int32 id) in C:\Users
\tqwhite\Documents\Visual Studio 2008\Projects\ExpressBook\ExpressBook
\Models\Student.cs:22
ExpressBook.Controllers.DistrictController.getStudentById(Int32 id)
in C:\Users\tqwhite\Documents\Visual Studio 2008\Projects\ExpressBook
\ExpressBook\Controllers\DistrictController.cs:22
lambda_method(ExecutionScope , ControllerBase , Object[] ) +79
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary`2 parameters) +178

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary`2
parameters) +24

System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
+52

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
filter, ActionExecutingContext preContext, Func`1 continuation) +254

System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c()
+19

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext, IList`1 filters, ActionDescriptor actionDescriptor,
IDictionary`2 parameters) +192

System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext, String actionName) +314
System.Web.Mvc.Controller.ExecuteCore() +105
System.Web.Mvc.ControllerBase.Execute(RequestContext
requestContext) +39

System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext
requestContext) +7
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
+21

System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult
_) +12

System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult
asyncResult) +44

System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) +7


System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+8679150

System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously) +155

Version Information: Microsoft .NET Framework Version:2.0.50727.4927;
ASP.NET Version:2.0.50727.4927

Diego Mijelshon

unread,
Mar 4, 2010, 5:54:26 PM3/4/10
to nhusers
How are you building your configuration?
More specifically, how are you adding the assembly?

   Diego



--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


tqwhite

unread,
Mar 4, 2010, 6:11:15 PM3/4/10
to nhusers
First, tell me where your kids are going to college so I can start
scholarships for them. I can't tell you how much I appreciate this
help.

Also, I should note, so you can interpret my dopiness properly, that
I'm new to C#, .NET, strongly typed languages and WIndows. I've been a
php guy for the last decade, however.

I don't know how I am building my configuration. I have a working .NET
MVC project. I referenced in nHibernate (and Lin Fu when I got an
error requesting it). I grabbed hibernate.cfg.xml from the download
and changed the database string. Is that the configuration you mean?

I checked my project's properties to make sure that I have the right
assembly name. Is this what you are talking about:<hibernate-mapping


xmlns="urn:nhibernate-mapping-2.2" assembly="ExpressBook"

namespace="ExpressBook">?

I suspect that I am missing the meaning of your questions. Please ask
me more.

thanks,
tqii

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >

Diego Mijelshon

unread,
Mar 4, 2010, 6:20:48 PM3/4/10
to nhusers
My kids might probably go to the public university, which is free in Argentina, but thanks anyway.

At some point in your code, you are creating a NHibernate.Cfg.Configuration instance, from which you use BuildSessionFactory() to get the sessionFactory you're using there.
You need to tell the configuration where to get the mappings from.
Since you already have the hbm.file(s) embedded in your assembly, you can either:
- use the <mapping assembly="Assembly name"/> in the hibernate.cfg.xml file
- call configuration.AddAssembly(assembly) with the appropriate assembly instance or name.

   Diego


To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

tqwhite

unread,
Mar 4, 2010, 10:48:51 PM3/4/10
to nhusers
You obviously have it too good in Argentina. You're going to have to
settle for my gratitude.

Your patient explanation put me on the right track. I couldn't get the
addAssembly version to work, but putting the mapping clause into the
config file worked great. I plowed through a couple of type issues and
Voila!, Security Exception.


[SecurityException: That assembly does not allow partially trusted
callers.]
NHibernate.ByteCode.LinFu.ProxyFactory..cctor() +0


I immediately went to my various bin directories and changed all of
the downloaded files so they are not blocked. No dice.

I surfed the web and read that this is a common problem for
nHibernate, that it requires full trust. I looked into my IIS7 setup
and it is set to "Full (internal)". I changed it to High but it made
no difference.

I googled about and found a lot of discussion of this problem. I added

<trust level="Medium" originUrl="" />

to the config. And this

using System.Security;
[assembly: AllowPartiallyTrustedCallers]

to the class that configures nHibernate. No improvement so I removed
them.

I don't know how to make my copy of Windows 7 operate as "Full Trust",
or honestly, what that means.

Any thoughts?

thanks,
tqii

> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup s.com>>

Harold Wanyama

unread,
Mar 5, 2010, 2:36:34 AM3/5/10
to nhu...@googlegroups.com
tqwhite like Diego said you need to set up your configuration(i havent seen u mention setting it up anywhere). This is such that nhibernate can be able to locate your mapping files(set as embedded resources) something of the sort...... Configuration cfg = new Configuration(); cfg.AddAssembly("assemblyName"). At least that IMO is the cause of all your problems.

To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

Jason Meckley

unread,
Mar 5, 2010, 8:42:09 AM3/5/10
to nhusers
you can also adjust your entity mappings like this

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ExpressBook"
namespace="ExpressBook.Services.BaseDataObjects">
<class name="StudentBase">
...
because you define the assembly and namespace in the root element you
can then use the short name when defining your mappings.
If you define the assembly in hibernate.config.xml, then you do not
need to add the assembly like Harold demonstrated above. If you are
not defining the assembly in the config file then the you need to add
the assembly when you instantiate Nh configuration.

I'm not familiar with Windows 7 or IIS 7 but I have read about the
medium trust issue. there is an NH add on to create static proxies
(rather than dynamic proxies) which is where the medium trust issue
occurs. I think there is a link on NHForge to this.

Since you are new to .net, C#, NH, and MVC I would recommend spiking
examples independent of each other. Once you feel comfortable with the
frameworks try integrating them together. for example:
spike a vanilla MVC project and play around with it until you feel you
understand how it works.
spike a console application to experiment with NH
spike a console application to experiment with C# symantics

then you can integrate NH into your MVC project. when you come across
an error you will be able to rule out some causes because you have a
better understanding of how each framework works.


On Mar 5, 2:36 am, Harold Wanyama <wanyal...@gmail.com> wrote:
> tqwhite like Diego said you need to set up your configuration(i havent seen
> u mention setting it up anywhere). This is such that nhibernate can be able
> to locate your mapping files(set as embedded resources) something of the
> sort...... Configuration cfg = new Configuration();
> cfg.AddAssembly("assemblyName"). At least that IMO is the cause of all your
> problems.
>

> ...
>
> read more »

tqwhite

unread,
Mar 5, 2010, 9:39:33 AM3/5/10
to nhusers
You guys are great. I really appreciate the help.

1) I have gotten past the mappings problem. I put a reference to the
assembly in the cfg file and have been able to make progress. I could
tell it worked because it complained that I had the wrong type
("int32" doesn't work, "int" does) and I was able to fix it.

2) Your advice is good, Jason, and I have been following it, going
step by step over the last several weeks. C# is still a foreign tongue
and I'm still completely confused by strong typing, but MVC and I are
old friends (that's what we've been doing in the php world for a
while).I have no need (that I know about) for console apps but have
got my MVC functioning smoothly. Also, mbUnit. Also, JavascriptMVC.

I have read like crazy searching for a solution to this trust issue
(I'm not lazing around in the hope that ssomeone will just solve my
problem, though I have hope ;-) ). Everything I have read so far seems
to want me to recompile some parts of nH but I will jettison it before
I do that. This project needs to be maintainable in the long run. I'm
not sure what static proxy means or what I am using for dynamic ones.
I guess this must be inside nH somewhere. I will look look look.

Answer me this, please. The web tells me that this has been a problem
since at least 2005. What OS are you folks running on? I am under the
impression that W7 is not much different than Vista or Windows Server
as far as security goes.

I remain lost on the Security Exception but, thanks again for your
help.

tqii

> ...
>
> read more »

Jason Meckley

unread,
Mar 5, 2010, 10:37:45 AM3/5/10
to nhusers
the security issue comes up alot when a website is deployed to a
public ISP. the ISP will not allow partially trusted code for security
reasons. usually with local, or internal development this is not an
issue.

NH uses proxies to manage lazy loading. The proxies are created using
Castle's Dynamic Proxy. It's transparent to your code. DP allows you
to create/alter functionality of an object at runtime. it gets into
AOP. You can replace the DP usage by utilizing the proxy generator add-
in. this is a post build (compile) event which would scan your NH
configuration and create a compiled proxy class which NH can use.

By using a console app to spike NH you can avoid the security
exception because everything runs local to your account. where as with
a web application you are running under a restricted account by
default.

> ...
>
> read more »

Richard Wilde

unread,
Mar 5, 2010, 9:50:30 AM3/5/10
to nhu...@googlegroups.com
>I remain lost on the Security Exception but, thanks again for your
>help.
>tqii

From an earlier email I seem to recall that you put <trust level="Medium"
... /> in your web.config.
This is going to cause you problems with NH, lazy loading and maybe several
other things, so I would take this out of the web.config

If you are going to deploy the application on a shared environment that uses
Medium Trust then obviously you will need the trust level. However I don't
think NH likes this too much!

Rippo

John Davidson

unread,
Mar 5, 2010, 11:28:49 AM3/5/10
to nhu...@googlegroups.com
the link below shows how to change trust levels in IIS7

http://technet.microsoft.com/en-us/library/cc753658(WS.10).aspx

John Davidson

tqwhite

unread,
Mar 5, 2010, 3:51:51 PM3/5/10
to nhusers
I'm starting to lose hope. So many smart people helping me and I still
can't make it work.

Yes, I did try the trust level in my web config but, when it didn't
help, I stripped it out. Nothing works either way.

This app will be deployed on a private server. I don't presently know
of any limitations in my ability to change its configuration to suit
the app (except for lack of skill, of course).

I am running this locally right now. I have 100% control over my
configuration, if only I can figure out what to do.

Thanks John for that link. My copy of IIS7 is running with the a trust
level of "Full (internal)". Just in case, I also tried it with "High
(web_hightrust.config)". It doesn't work in either case. (I also tried
it with medium, low and minimal. No dice, though the latter two told
me I can't debug instead.)

I also note that I a running this app out of my personal Documents
directory and that I had to add IUSR and IIS_USRS to its security list
before IIS7 would serve the site.

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >

tqwhite

unread,
Mar 5, 2010, 3:57:58 PM3/5/10
to nhusers
Also, I note that the system complained that I had not included a
reference to Lin Fu but did not complain that I had not provided a
reference to Castle, even though I see Castle in the same directory
(for lazy loading) as Lin Fu.

Any meaning to that?

tqii

On Mar 5, 10:28 am, John Davidson <jwdavid...@gmail.com> wrote:

> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >

Michael A. Bell

unread,
Mar 5, 2010, 4:06:50 PM3/5/10
to nhu...@googlegroups.com
I'm jumping into this a little late so I have missed some of the previous posts, but if you did not build this via the source and just downloaded the assemblies themselves, you might need to check the file properties on the assemblies and make sure that in the bottom right corner the Unblock button does not exist.

If it does, this may be why you are receiving this error. This is a windows security feature since vista for files that are downloaded from the web and could potentially be harmful, like exes, dlls and other scripts. Try clicking the Unblock button and see if that fixes it for you. I have run into a similar issue before and that did the trick. I have also seen cases where you click the unblock button in the file properties and it still does not unblock the file. If that is the case for you my only suggestion to get you up and on your feet would be to build the source yourself.

Mike

Any meaning to that?

tqii

To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

tqwhite

unread,
Mar 5, 2010, 5:25:45 PM3/5/10
to nhusers
Thanks Michael. I have done unblocking (and am mad that it doesn't
give me an opportunity to unblock enclosed directories; makes this
very error prone).

I am here to announce that I have had SUCCESS!!!!!

I am a little sad to say that I don't know why, but only a very, very
little.

Here's what happened.

After taking some time off for other activities, I wanted to make sure
that I had told the truth when I said I had exercised IIS7's trust
levels (I knew I had, but, what the heck). So, I did them again. I
started with Minimal and, one by one, worked my way back up to High.
At the low levels, it complained that I had debugging turned on. At
High, it gave me a security exception. I set it back to Full without
looking at another error message.

Then, I was reading something that told me that the reason I had my
properties set to "virtual" is that this supports lazy loading. Since
you've kindly told me that lazy loading does proxies and proxies
appear to be related to my problem (can't remember why I think this),
I thought, Screw those "virtual" declarations and removed them. This
got me an error message explaining that they were necessary.

I replaced them and retested. The error message was "Invalid
object." !!!!!!!

I'm start cursing in my head. I figure that I had somewhere made a
typo while thrashing around and I have absolutely no idea where it
could be. The worst possible situation.

So, I start to debug. I look for typos in the files I can remember
touching today (not too many because I'm stuck with nHibernate).
Nothing wrong. I start googling the error, nothing.

I grab the entire error message and view it in a browser so I can read
it properly. Damned if the error is not from nHibernate. It's from
sql, "SqlException (0x80131904): Invalid object name." I get excited.
If it's getting to sql, then it's opening nHibernate objects.

Something suggests table mapping problems. Sure enough, my table and
class names don't match. I add a table attribute to the mapping file.
No joy.

But, having gone through the investigation to figure out how
nHibernate knows table names, I thought, How the heck does it know the
Database name?

I had simply copied my connection string out of a VIsual Studio
connection's property, so I had not really paid attention to it. Sure
as heck, the Initial Catalog was set to a different database!!

Changed, tested, Eureka!!!

Strictly speaking, I suppose, I don't know that nHibernate is working
(so I may be back), but it is no longer crashing. Neither the trust
experiments or the removing and replacing the virtual server should
have changed anything. It doesn't really matter.

All of you folks that so patiently explained things to me, I can't
thank you enough. I would do it personally if I could, but I have to
ask you each to buy yourself a tasty cocktail this weekend and pretend
that I am raising a glass in your honor.

tqii


On Mar 5, 3:06 pm, "Michael A. Bell" <mb...@interactivesoftworks.com>
wrote:

John Davidson

unread,
Mar 5, 2010, 6:24:37 PM3/5/10
to nhu...@googlegroups.com
Now you may want to get log4net working and turn on show-sql in nhibernate, which will write to log4net. This records all the slq statements generated by nhibernate and sent to the database.

John Davidson

tqwhite

unread,
Mar 5, 2010, 10:39:31 PM3/5/10
to nhusers
I wants me some of that logging action. Can you tell me the basic
outline of how I would do that?

tqii

http://darkside.posterious.com

> > > >http://technet.microsoft.com/en-us/library/cc753658(WS.10).aspx<http://technet.microsoft.com/en-us/library/cc753658%28WS.10%29.aspx>

> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup s.com>>

John Davidson

unread,
Mar 6, 2010, 7:14:56 AM3/6/10
to nhu...@googlegroups.com

tqwhite

unread,
Mar 6, 2010, 11:50:56 AM3/6/10
to nhusers

Cool. I'm sure I would have found them sooner or later, as I do this
high octane learning experience, I'm getting good at that, but I would
doubtless had to look at fifteen lame ones first. Thanks for saving my
time.

best,
tqii


On Mar 6, 6:14 am, John Davidson <jwdavid...@gmail.com> wrote:
> @ good references are
>
> [1]  http://www.beansoftware.com/asp.net-tutorials/nhibernate-log4net.aspx

> [2]http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/07/01/how-...

> > > > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup s.com>


> > <nhusers%252Bunsubscribe@googlegroup s.com>>
> > > > > > > .
> > > > > > > For more options, visit this group at
> > > > > > >http://groups.google.com/group/nhusers?hl=en.
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "nhusers" group.
> > > > > To post to this group, send email to nhu...@googlegroups.com.
> > > > > To unsubscribe from this group, send email to
> > > > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >
> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup s.com>>
> > > > .

Fabio Maulo

unread,
Mar 6, 2010, 11:59:05 AM3/6/10
to nhu...@googlegroups.com
I think you are using a mixed salad with chorizo, dulce de leche, yoghurt, a little bit of whiskey, fish ans so on.  

2010/3/5 tqwhite <tqw...@gmail.com>
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Reply all
Reply to author
Forward
0 new messages