Edit: The below information was the symptom that led me to the following tests. I downloaded, compiled, and ran all the tests for NHibernate under Release mode. They all work. I fired up NH Profiler and looked at the difference between the logs in Debug and Release. Release didn’t appear to be executing any SQL besides the transaction open and close statements. Verified that fact with SQL profiler.
So NHibernate can connect to the database and execute transaction begin / end statements ok, and I can connect and execute arbitrary SQL via the normal ADO methods as well, but NHibernate can NOT execute any other SQL under Release mode.
My session is opened and closed at the beginning and end of each request via an HttpModule, which uses a SessionFactory singleton inside a UnitOfWork implementation which is cached in HttpContext by Structuremap.
It all works wonderfully under Debug.
Also, changing my calling code to make sure that the first called NHibernate method is a Load returns this to me:
Unable to locate persister: Domain.Model.User
Edit 2:
Here’s the stack trace…
[HibernateException: Unable to locate persister: STEP.Domain.Model.User]
NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:58
NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:2466
NHibernate.Impl.SessionImpl.Load(String entityName, Object id) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1213
NHibernate.Impl.SessionImpl.Load(Type entityClass, Object id) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1242
NHibernate.Impl.SessionImpl.Load(Object id) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1166
STEP.Persistence.Repositories.RepositoryWithTypedId`2.Load(TId id) in C:\Projects\STEP Handbook Dev\src\STEP.Persistence\Repositories\Repository.cs:80
STEP.Website.Providers.STEPNHibernateMembershipProvider.ValidateUser(String username, String password) in C:\Projects\STEP Handbook Dev\src\STEP.Website\Providers\STEPNHibernateMembershipProvider.cs:490
STEP.Website.Controllers.AccountController.LogOn(LogOnViewModel model, String returnUrl) in C:\Projects\STEP Handbook Dev\src\STEP.Website\Controllers\AccountController.cs:759
lambda_method(Closure , ControllerBase , Object[] ) +179
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +785306
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +785306
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +785306
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +785306
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +785360
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371
And the code that triggers this is just a simple Session.Load(id) call to NHibernate.
Original:
When running NHibernate 3.1, I am getting an “Index was out of range” exception, but only when compiling in Release mode. I’m calling NHibernate here:
var results = Session.Query<User>.Where(predicate);
Then right below this, I am checking to see how many get returned (As this is actually my FindOne() method in my repository, and I need to make sure I only get one back). So I am calling results.Count(), which is triggering the NHibernate query to execute.
My stack trace shows that this is happening in NhQueryprovider.ExecuteQuery at the last line of the method (Which is “return results[0]”). The only thing I can get from this is that apparently NHibernate is getting an empty list back there (It shouldn’t there should be at least one result returned here…) which is causing this.
This COULD be a bug in the NHibernate.Linq stuff that hasn’t been caught yet, as I believe that just went native to Nhibernate in 3.x… Anyone else see this?
I figured it out. The issue, as pointed to by the error about persisters, was that NHibernate was not getting any mapping files in Release mode.
The reason turned out to be because when I was configuring Fluent NHibernate with a PersistenceModel, I was calling
PersistenceModel.AddMappingsFromThisAssembly()instead ofPersistenceModel.AddMappingsFromAssembly(Assembly). While the former worked fine in Debug mode, in Release mode it apparently does not.Changing my initialization code to use the latter fixed all my issues. The original issue, with the out of range exception, seems to be a failing on NHibernate.LINQ’s part to account for this exceptional state.