温莎城堡休眠设施延迟加载
我正在迁移我的 mvc Web 应用程序以使用 Nhibernate Facility 进行管理 休眠会话。
我遇到了这个奇怪的问题;
当我通过以下方式在 web.config/ httpmodules 下注册 SessionWebModule 时:
<add name="NHibernateSessionWebModule"
type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule,
Castle.Facilities.NHibernateIntegration" />
系统无法延迟加载对象。我没有得到会话异常。
如果我不将其添加到 web.config 延迟加载工作正常。 就注册温莎而言,一切都似乎没问题 容器并注册 Nhibernate Facility 即可。休眠 设施能够提供会话管理器,并且 Windsor 容器是 能够解析对象。
当然,如果没有 SessionWebModule,Nhibernate 工具几乎毫无用处 对我来说,在这种情况下它无法正确管理 会议。我看到 datareader 已准备好开放类型异常,它们是 不好..
我被困住了,需要紧急解决这个问题,我会的 感谢您的任何帮助。
我在下面包含了我的配置和代码的一些部分 提供更多信息
Web.config:
<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>
Global.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]- 无法初始化代理 - 无会话。] NHibernate.Proxy.AbstractLazyInitializer.Initialize() +138 NHibernate.Proxy.AbstractLazyInitializer.GetImplementation() +37 NHibernate.ByteCode.LinFu.LazyInitializer.Intercept(调用信息 信息)+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儿童)+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儿童)+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(布尔值 includeStagesBeforeAsyncPoint、布尔值 includeStagesAfterAsyncPoint) +1266
I am migrating my mvc web app to use Nhibernate Facility to manage
Nhibernate sessions.
I am encountering this strange problem;
When I register SessionWebModule under web.config/ httpmodules via:
<add name="NHibernateSessionWebModule"
type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule,
Castle.Facilities.NHibernateIntegration" />
System is unable to lazy load objects. I get no session exception.
If I don't add this to web.config lazy loading is working fine.
Everything part from this seems to be OK as far as regitering Windsor
container and registering Nhibernate Facility goes. Nhibernate
Facility is able to provide session manager and Windsor container is
able to resolve objects.
Ofcourse, without SessionWebModule Nhibernate Facility is near useless
for me as in this situation it is not able to properly manage
sessions. I see datareader is allready open type exceptions which are
no good..
I am stuck and need to urgently get past this problem I will
appreciate any help from you.
I am including below some sections of my configuration and code to
give more information
Web.config:
<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>
Global.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));
}
}
Lazy loading exception:
[LazyInitializationException: Initializing[AppWeb.Domain.City#31135]-
Could not initialize proxy - no Session.]
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.RenderControlInternal(HtmlTextWriter writer,
ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
ControlAdapter adapter) +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 adapter) +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)
+1266
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是设计使然。您必须保持会话开放。
如果你正在做网络,我建议你在请求开始时打开会话并在结束时关闭它。
从本文中获取示例: http://www.codeproject.com/KB/architecture /NHibernateBestPractices.aspx
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
如果您使用 MVC,我使用操作过滤器来关闭我的 nhibernate 会话
SessionSource 是我管理创建会话的地方...并且 SessionSource.EndContextSession() 方法本质上调用:
If you're using MVC, I use an action filter to close my nhibernate sessions
SessionSource is where I'm managing creating my sessions... and the SessionSource.EndContextSession() method essentially calls:
我相信你必须将 isWeb 属性设置为 true:
I belive you have to set the
isWeb
attribute to true: