2

我正在迁移我的 mvc Web 应用程序以使用 Nhibernate Facility 来管理 Nhibernate 会话。

我遇到了这个奇怪的问题;

当我通过以下方式在 web.config/ httpmodules 下注册 SessionWebModule 时:

<add name="NHibernateSessionWebModule"
type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule,
Castle.Facilities.NHibernateIntegration" />

系统无法延迟加载对象。我没有任何会话异常。

如果我不将此添加到 web.config 延迟加载工作正常。就重新注册 Windsor 容器和注册 Nhibernate Facility 而言,这一切似乎都还可以。Nhibernate Facility 能够提供会话管理器,Windsor 容器能够解析对象。

当然,没有 SessionWebModule Nhibernate Facility 对我来说几乎没用,因为在这种情况下它无法正确管理会话。我看到 datareader 已经是开放类型的异常,这不好..

我被卡住了,需要紧急解决这个问题,我将不胜感激你的任何帮助。

我在下面包含我的配置和代码的一些部分以提供更多信息

网络配置:

<castle>
        <facilities>
            <facility id="nhibernatefacility" isWeb="false"
type="Castle.Facilities.NHibernateIntegration.NHibernateFacility,
Castle.Facilities.NHibernateIntegration">
                <factory id="nhibernate.factory">
                    <settings>
                        <item
key="connection.provider">NHibernate.Connection.DriverConnectionProvider</
item>
                        <item
key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
                        <item key="connection.connection_string">Data
Source=zzz;Database=xxx;Trusted_Connection=True;</item>
                        <item
key="dialect">NHibernate.Dialect.MsSql2005Dialect</item>
                        <item
key="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHibernate.ByteCode.LinFu</item>
                      </settings>
                    <assemblies>
                        <assembly>AppWeb.Domain</assembly>
                    </assemblies>
                </factory>
            </facility>
        </facilities>
    </castle>

<system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true">
              <add name="NHibernateSessionWebModule"
type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule,
Castle.Facilities.NHibernateIntegration" />
        </modules>
</sytem.webServer>

全球.asax

public class MvcApplication : System.Web.HttpApplication,
IContainerAccessor
    {
        private static IWindsorContainer container;

        public IWindsorContainer Container
        {
            get { return container; }
        }

        void Application_Error(Object sender, EventArgs e)
        {
            Logger.Error(Server.GetLastError());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            #region Ignores
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("admin/{*url}");
            #endregion

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Main", id = "" },
                new { controller = @"[^\.]*" }
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            log4net.Config.XmlConfigurator.Configure();
            SetupWindsor();
        }

        private void SetupWindsor()
        {
            container = new WindsorContainer(new XmlInterpreter());

            container.Register(
                        AllTypes
                            .FromAssemblyContaining<UserRepository>()
                            .Where(t => t.Name.EndsWith("Repository",
StringComparison.Ordinal))
                            .WithService
                            .FirstInterfaceOnType()
                            .Configure(r =>
r.LifeStyle.PerWebRequest),

AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
                            .BasedOn<IController>()
                            .Configure(c =>
c.LifeStyle.PerWebRequest),

                    );
                    //.AddFacility<TransactionFacility>();

            ControllerBuilder.Current.SetControllerFactory(new
WindsorControllerFactory(container));
        }
    }

延迟加载异常:

[LazyInitializationException: Initializing[AppWeb.Domain.City#31135]- 无法初始化代理 - 没有会话。] NHibernate.Proxy.AbstractLazyInitializer.Initialize() +138 NHibernate.Proxy.AbstractLazyInitializer.GetImplementation() +37 NHibernate.ByteCode.LinFu .LazyInitializer.Intercept(InvocationInfo info) +72 CityProxy.get_Name() +143 ASP.views_tour_filters_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\wwwroot\AppWeb\AppWeb.Web\Views\Tour \Filters.ascx:14 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+256 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)+19 System.Web.UI.Control.Render(HtmlTextWriter writer)+10 System.Web.UI.Control.Render(HtmlTextWriter writer) Web.UI.Control.RenderControlInternal(HtmlTextWriter 编写器,ControlAdapter 适配器)+27 System.Web.UI.Control。RenderControl(HtmlTextWriter writer,ControlAdapter 适配器)+99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+134 System.Web.UI.Control .RenderChildren(HtmlTextWriter writer) +19 System.Web.UI.Page.Render(HtmlTextWriter writer) +29 System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer) , ControlAdapter 适配器) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter 适配器) +99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +1266ControlAdapter 适配器)+99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+134 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer ) +19 System.Web.UI.Page.Render(HtmlTextWriter writer) +29 System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter 适配器) + 27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266ControlAdapter 适配器)+99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+134 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer ) +19 System.Web.UI.Page.Render(HtmlTextWriter writer) +29 System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter 适配器) + 27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 System.Web.UI.Page.Render(HtmlTextWriter writer) +29 System.Web.Mvc.ViewPage.Render( HtmlTextWriter 编写器)+59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter 编写器,ControlAdapter 适配器)+27 System.Web.UI.Control.RenderControl(HtmlTextWriter 编写器,ControlAdapter 适配器)+99 System.Web.UI.Control.RenderControl (HtmlTextWriter writer)+25 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)+1266RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 System.Web.UI.Page.Render(HtmlTextWriter writer) +29 System.Web.Mvc.ViewPage.Render( HtmlTextWriter 编写器)+59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter 编写器,ControlAdapter 适配器)+27 System.Web.UI.Control.RenderControl(HtmlTextWriter 编写器,ControlAdapter 适配器)+99 System.Web.UI.Control.RenderControl (HtmlTextWriter writer)+25 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)+1266渲染(HtmlTextWriter 编写器)+59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter 编写器,ControlAdapter 适配器)+27 System.Web.UI.Control.RenderControl(HtmlTextWriter 编写器,ControlAdapter 适配器)+99 System.Web.UI.Control .RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+1266渲染(HtmlTextWriter 编写器)+59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter 编写器,ControlAdapter 适配器)+27 System.Web.UI.Control.RenderControl(HtmlTextWriter 编写器,ControlAdapter 适配器)+99 System.Web.UI.Control .RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+1266

4

3 回答 3

3

It's by design. You have to keep your session open.

If you are doing web, I suggest you to open the session at the begin of the request and close it at the end.

Get the samples from this article: http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx

于 2010-06-29T12:40:40.127 回答
0

如果您使用的是 MVC,我会使用操作过滤器来关闭我的休眠会话

public class NHibernateSessionAttribute : ActionFilterAttribute
{
    public NHibernateSessionAttribute()
    {
        Order = 100;
    }


    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (filterContext.IsChildAction) return;

         SessionSource.EndContextSession();
    }
}

SessionSource 是我管理创建会话的地方......并且 SessionSource.EndContextSession() 方法本质上调用:

    public static void EndContextSession()
    {
        var session = CurrentSessionContext.Unbind(Factory);
        if (session != null && session.IsOpen)
        {
            try
            {
                if (session.Transaction != null && session.Transaction.IsActive)
                {
                    // an unhandled exception has occurred and no db commit should be made
                    session.Transaction.Rollback();
                }
            }
            finally
            {
                session.Close();
                session.Dispose();
            }
        }
    }
于 2011-03-17T16:40:08.597 回答
0

我相信您必须将isWeb属性设置为true:

<facility id="nhibernatefacility" isWeb="true" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration">
于 2011-05-09T18:02:41.267 回答