4

我正在使用 ASP.Net MVC 3、spring.net 1.3.2 和 nhibernate 3.2。我一直在努力让 Web 应用程序在 IIS 7.5 中运行。它在 Visual Studio 2010 中运行良好。

我得到的错误是

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: NHibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here]
   Spring.Data.NHibernate.SpringSessionContext.CurrentSession() in c:\_prj\spring-net\trunk\src\Spring\Spring.Data.NHibernate12\Data\NHibernate\SpringSessionContext.cs:72
   Technolog.Rma.Business.Repositories.RepairBatchDAO.findRepairBatchPageCount(String login, Int32 rowsPerPage) in C:\work\samplemvc\src\Technolog.Rma.Business\Repositories\RepairBatchDAO.cs:55
   _dynamic_Technolog.Rma.Business.Repositories.RepairBatchDAO.findRepairBatchPageCount(Object , Object[] ) +273
   Spring.Reflection.Dynamic.SafeMethod.Invoke(Object target, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Reflection\Dynamic\DynamicMethod.cs:156
   Spring.Aop.Framework.DynamicMethodInvocation.InvokeJoinpoint() in c:\_prj\spring-net\trunk\src\Spring\Spring.Aop\Aop\Framework\DynamicMethodInvocation.cs:100
   Spring.Dao.Support.PersistenceExceptionTranslationInterceptor.Invoke(IMethodInvocation invocation) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Support\PersistenceExceptionTranslationInterceptor.cs:181

<snip/>

我的 web.config 文件是

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368

  Spring.Context.Support.ContextHandler, Spring.Core

    <resource uri="~/Config/propertyConfigurer.xml" />
  -->
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc3" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="dataFile" value="App_Data\data.db3" />
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="SessionFactory"/>
  </appSettings>
  <spring>
    <context>
      <resource uri="file://~/Config/propertyConfigurer.xml" />
      <resource uri="file://~/Config/sessionFactory.xml" />
      <resource uri="file://~/Config/mappers.xml" />
      <resource uri="file://~/Config/repositories.xml" />
      <resource uri="file://~/Config/controllers.xml" />
      <resource uri="file://~/Config/scopedobjects.xml" />
      <resource uri="file://~/Config/utils.xml" />
    </context>
  </spring>
  <system.web>
    <httpModules>
      <!--
      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
      <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32"/> 
      -->
    </httpModules>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <customErrors mode="Off"/>

  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
      <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32"/>
    </modules>
    <!--
    <handlers>
      <add name="SpringPageHandler" verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
      <add name="SpringWebServiceHandler" verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
      <add name="SpringContextMonitor" verb="*" path="ContextMonitor.ashx" type="Spring.Web.Support.ContextMonitor, Spring.Web"/>
    </handlers>
    -->
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.7.0" newVersion="4.0.7.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我的 sessionFactory.xml 文件是

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" 
         xmlns:db="http://www.springframework.net/database"
         xmlns:tx="http://www.springframework.net/tx">

  <object
    id="siteRoot"
    type="System.Web.Hosting.HostingEnvironment, System.Web"
    factory-method="get_ApplicationPhysicalPath" />

  <object
    id="dataFile"
    type="System.IO.Path, mscorlib"
    factory-method="Combine">
    <constructor-arg name="path1" ref="siteRoot" />
    <constructor-arg name="path2" value="${dataFile}" />
  </object>

  <db:provider id="dbProvider"
               provider="System.Data.SqlClient"
               connectionString="Data Source=AJASQL7;Initial Catalog=Xanadu;User ID=RMA;Password=RMA;"/>


  <object id="sessionFactory" scope="application" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
    <property name="DbProvider" ref="dbProvider"/>
    <property name="MappingAssemblies">
      <list>
        <value>Technolog.Rma.Business</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
        <entry key="dialect" value="NHibernate.Dialect.MsSql7Dialect"/>
        <entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
        <entry key="use_proxy_validator" value="false" />
        <entry key="current_session_context_class" value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate32"/>
      </dictionary>
    </property>

  </object>

  <!-- Transaction Management Strategy - local database transactions -->

  <object id="transactionManager"
        type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
    <property name="DbProvider" ref="dbProvider"/>
    <property name="SessionFactory" ref="sessionFactory"/>
  </object>

  <!-- Exception translation object post processor -->
  <object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>

</objects>

我的 RepairBatchDao 的一部分是

[Repository]
class RepairBatchDAO : IRepairBatchDAO
{

    public ISessionFactory SessionFactory { get; set; }

    public long findRepairBatchPageCount(string login, int rowsPerPage)
    {
        var rowCount = 1L;
        var session = SessionFactory.GetCurrentSession(); // line 55
        var transaction = session.BeginTransaction();
        try
        {
            transaction = session.BeginTransaction();
            var hqlCountQuery = session.CreateQuery("select count(*) from Technolog.Rma.Business.Domain.RepairBatch rb where rb.Customer.Login=?")
                            .SetParameter(0, login);
            rowCount = hqlCountQuery.UniqueResult<long>();
            transaction.Commit();
            return (rowCount + rowsPerPage - 1) / rowsPerPage;
        }
        catch
        {
            if (transaction != null) transaction.Rollback();
        }
        finally
        {
            if (session != null) session.Close(); // I shouldn't be doing this...
        }

        return 0L;

    }

    // snip ...

我花了太多时间试图自己解决这个问题。我希望有人能指出我正确的方向。我已经检查了 stackoverflow 中的各种帖子(太多了,无法提及),但它们没有帮助。

我是一个 Java 人,这是我的第一个 .net 项目,所以不确定我是否做了一些愚蠢的事情。

25/9/2012

我已经对我的 customerDAO 进行了更改

 public class CustomerDAO : ICustomerDAO
    {

        public ISessionFactory SessionFactory { get; set; }

        private TransactionTemplate tt;

        public IPlatformTransactionManager  TransactionManager
        {
            set { tt = new TransactionTemplate(value); }
        }
 <snip/>


        public Customer find(string login) 
        {
            IList<Customer> customers;
            Object result = tt.Execute(delegate(ITransactionStatus status) //line 42
            {
                var transaction = status.Transaction;

                  var session = this.SessionFactory.GetCurrentSession();
                  //var transaction = session.BeginTransaction();
                  try
                  {
                      customers = session.CreateQuery("from Technolog.Rma.Business.Domain.Customer Customer where Customer.Login=?")
                          .SetParameter(0, login)
                          .List<Customer>();

                      if (customers.Count > 0)
                      {
                          return customers[0];
                      }
                  }
                  catch
                  {

                  }
                  finally
                  {
                      if (session != null) session.Close();
                  }

                  return null;
              });


            return (Customer)result;

        }

我现在得到的错误是

Server Error in '/RMA' Application.
--------------------------------------------------------------------------------

Cannot access a disposed object.
Object name: 'AdoTransaction'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'AdoTransaction'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[ObjectDisposedException: Cannot access a disposed object.
Object name: 'AdoTransaction'.]
   NHibernate.Transaction.AdoTransaction.Commit() +425
   Spring.Data.NHibernate.HibernateTransactionManager.DoCommit(DefaultTransactionStatus status) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data.NHibernate\Data\NHibernate\HibernateTransactionManager.cs:579
   Spring.Transaction.Support.AbstractPlatformTransactionManager.ProcessCommit(DefaultTransactionStatus status) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Transaction\Support\AbstractPlatformTransactionManager.cs:853
   Spring.Transaction.Support.TransactionTemplate.Execute(TransactionDelegate transactionMethod) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Transaction\Support\TransactionTemplate.cs:159
   Technolog.Rma.Business.Repositories.CustomerDAO.find(String login) in C:\work\samplemvc\src\Technolog.Rma.Business\Repositories\CustomerDAO.cs:42
   _dynamic_Technolog.Rma.Business.Repositories.CustomerDAO.find(Object , Object[] ) +147
   Spring.Reflection.Dynamic.SafeMethod.Invoke(Object target, Object[] arguments) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Reflection\Dynamic\DynamicMethod.cs:156
   Spring.Aop.Framework.DynamicMethodInvocation.InvokeJoinpoint() in c:\_prj\spring-net\trunk\src\Spring\Spring.Aop\Aop\Framework\DynamicMethodInvocation.cs:100
   Spring.Dao.Support.PersistenceExceptionTranslationInterceptor.Invoke(IMethodInvocation invocation) in c:\_prj\spring-net\trunk\src\Spring\Spring.Data\Dao\Support\PersistenceExceptionTranslationInterceptor.cs:190
   CompositionAopProxy_33677284ca854c0b8c01a5b0ae0a0524.find(String login) +383
   Technolog.Rma.Web.Controllers.HomeController.Index(Int32 page) in C:\work\samplemvc\src\Technolog.Rma.Web\Controllers\HomeController.cs:36
   lambda_method(Closure , ControllerBase , Object[] ) +112
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +248
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +125
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +640
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +312
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +691
   System.Web.Mvc.Controller.ExecuteCore() +162
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +305
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +469
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 
4

2 回答 2

1

根据上面的评论,我遵循了实现 osiv 的说明。我还发现我正在关闭一个会话,而我应该让它保持打开状态,从而导致

Cannot access a disposed object.

我使用 OSIV 的 web.config 文件是

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368

  Spring.Context.Support.ContextHandler, Spring.Core

    <resource uri="~/Config/propertyConfigurer.xml" />
  -->
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc3" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="dataFile" value="App_Data\data.db3" />
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="SessionFactory"/>
  </appSettings>
  <spring>
    <context>
      <resource uri="file://~/Config/propertyConfigurer.xml" />
      <resource uri="file://~/Config/sessionFactory.xml" />
      <resource uri="file://~/Config/mappers.xml" />
      <resource uri="file://~/Config/repositories.xml" />
      <resource uri="file://~/Config/controllers.xml" />
      <resource uri="file://~/Config/scopedobjects.xml" />
      <resource uri="file://~/Config/utils.xml" />
    </context>
  </spring>
  <system.web>
    <httpModules>

      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
      <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32"/> 

    </httpModules>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <customErrors mode="Off"/>

  </system.web>
  <system.webServer>

    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">

      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
      <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32"/>
    </modules>
    <!--
    <handlers>
      <add name="SpringPageHandler" verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
      <add name="SpringWebServiceHandler" verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
      <add name="SpringContextMonitor" verb="*" path="ContextMonitor.ashx" type="Spring.Web.Support.ContextMonitor, Spring.Web"/>
    </handlers>
    -->
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.7.0" newVersion="4.0.7.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

请注意,对 osiv 和 spring WebSupportModule 的引用位于 / 中以使其在我的开发环境中工作,并且在 / 中以使应用程序在 IIS 7.5 中工作

我也在我的视图中使用延迟加载,所以 osiv 是更好的解决方案。我在我的问题中展示了程序化事务控制,因为这就是我试图提出解决方案的程度。

于 2012-09-27T09:23:02.070 回答
0

您已经配置了一个TransactionManager,但您没有使用它。在调用之前SessionFactory.GetCurrentSession();,请尝试在您的事务管理器上启动事务。当使用 Spring 的事务管理时,你不应该显式地使用 NHibernate 的事务支持。

您可以以编程方式或声明方式执行此操作;在这个阶段,我会先尝试程序化方法。

于 2012-09-25T07:06:59.577 回答