我正在迁移我的mvc Web应用程序以使用Nhibernate Facility进行管理 Nhibernate会议。
我遇到了这个奇怪的问题;
当我在web.config / httpmodules下注册SessionWebModule时:
<add name="NHibernateSessionWebModule"
type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule,
Castle.Facilities.NHibernateIntegration" />
系统无法延迟加载对象。我没有会话异常。
如果我不将此添加到web.config延迟加载工作正常。 就温莎而言,一切都是好的 容器和注册Nhibernate Facility去。 NHibernate的 设施能够提供会话管理器和Windsor容器 能够解决对象。
当然,没有SessionWebModule Nhibernate Facility几乎没用 对我而言,在这种情况下,它无法正确管理 会话。我看到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(InvocationInfo info)+72 CityProxy.get_Name()+ 143 ASP.views_tour_filters_ascx .__ Render__control1(HtmlTextWriter __w, 在c:\ wwwroot \ AppWeb \ AppWeb.Web \ Views \ Tour中控制parameterContainer) \ Filters.ascx:14 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection孩子)+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适配器)+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孩子)+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(布尔 includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) +1266
答案 0 :(得分:3)
这是设计的。你必须保持你的会话开放。
如果您正在使用网络,我建议您在请求开始时打开会话,并在结束时关闭它。
获取本文中的示例:http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx
答案 1 :(得分:0)
如果你正在使用MVC,我使用动作过滤器关闭我的nhibernate会话
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();
}
}
}
答案 2 :(得分:0)
我认为您必须将isWeb
属性设置为true:
<facility id="nhibernatefacility" isWeb="true" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration">