gpt4 book ai didi

c# - CaSTLe Windsor Nhibernate Facility 延迟加载

转载 作者:行者123 更新时间:2023-11-30 22:45:16 25 4
gpt4 key购买 nike

我正在迁移我的 mvc web 应用程序以使用 Nhibernate Facility 来管理休眠 session 。

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

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

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

系统无法延迟加载对象。我没有得到任何 session 异常。

如果我不将它添加到 web.config 中,延迟加载工作正常。就注册温莎而言,这一切似乎都还可以容器并注册 Nhibernate Facility。 hibernate 设施能够提供 session 管理器和 Windsor 容器能够解析对象。

当然,如果没有 SessionWebModule,Nhibernate Facility 几乎毫无用处对我来说,在这种情况下,它无法妥善管理 session 。我看到 datareader 是 allready open type exceptions 这是不好..

我被困住了,需要紧急解决这个问题我会的感谢您的任何帮助。

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

网络配置:

<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: 初始化 [AppWeb.Domain.City#31135]-无法初始化代理 - 没有 session 。] NHibernate.Proxy.AbstractLazyInitializer.Initialize() +138 NHibernate.Proxy.AbstractLazyInitializer.GetImplementation() +37 NHibernate.ByteCode.LinFu.LazyInitializer.Intercept(InvocationInfo信息)+72 CityProxy.get_Name() +143 ASP.views_tour_filters_ascx.__Render__control1(HtmlTextWriter __w,控制参数Container)在c:\wwwroot\AppWeb\AppWeb.Web\Views\Tour\过滤器.ascx:14 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter 编写器,ICollection child )+256 System.Web.UI.Control.RenderChildren(HtmlTextWriter 作者)+19 System.Web.UI.Control.Render(HtmlTextWriter 编写器)+10 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter 编写器,ControlAdapter适配器)+27 System.Web.UI.Control.RenderControl(HtmlTextWriter 编写器,ControlAdapter适配器)+99 System.Web.UI.Control.RenderControl(HtmlTextWriter 编写器)+25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter 编写器,ICollection child )+134 System.Web.UI.Control.RenderChildren(HtmlTextWriter 作者)+19 System.Web.UI.Page.Render(HtmlTextWriter 编写器)+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 编写器)+25 System.Web.UI.Page.ProcessRequestMain( bool 值includeStagesBeforeAsyncPoint, bool 值 includeStagesAfterAsyncPoint)+1266

最佳答案

这是设计使然。您必须保持 session 打开。

如果你做的是web,我建议你在请求开始时打开session,在请求结束时关闭。

从这篇文章中获取示例:http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx

关于c# - CaSTLe Windsor Nhibernate Facility 延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3140534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com