gpt4 book ai didi

asp.net-mvc - NHibernate/MVC延迟加载因“session closed or no session”而失败

转载 作者:行者123 更新时间:2023-12-02 04:09:00 26 4
gpt4 key购买 nike

我在ASP.NET MVC应用程序后面使用NHibernate,尝试通过AJAX调用保存对象时遇到了令人沮丧的问题。我越来越平常:

无法延迟初始化角色集合:[类型]没有 session 或 session 已关闭

问题是,据我所知 session 未关闭。我正在使用HttpModule在NHibernate设置为使用网络 current_session_context_class的情况下按请求模式处理每个 session 的 session 。这是HttpModule:

public class NHHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.EndRequest += ApplicationEndRequest;
context.BeginRequest += ApplicationBeginRequest;
}

public void ApplicationBeginRequest(object sender, EventArgs e)
{
CurrentSessionContext.Bind(SessionFactory.GetNewSession());
}

public void ApplicationEndRequest(object sender, EventArgs e)
{
var currentSession = CurrentSessionContext.Unbind(SessionFactory.GetSessionFactory());
currentSession.Close();
currentSession.Dispose();
}

public void Dispose()
{
// Do nothing
}
}

我的SessionFactory也很标准:
public static class SessionFactory
{
private static ISessionFactory _sessionFactory;

private static void Init()
{
_sessionFactory = Fluently.Configure() //Lots of other stuff here for NH config
.BuildSessionFactory();
}

public static ISessionFactory GetSessionFactory()
{
if (_sessionFactory == null)
Init();

return _sessionFactory;
}

public static ISession GetNewSession()
{
return GetSessionFactory().OpenSession();
}

public static ISession GetCurrentSession()
{
return GetSessionFactory().GetCurrentSession();
}
}

我正在使用工作单元进行事务处理,这就是为什么我不在BeginRequest处打开事务的原因。即使这样,我也尝试过,结果没有变化。

我正在尝试通过AJAX帖子将 Comment对象保存到 User对象。这是 Controller 代码:
    [HttpPost, ValidateInput(false)]
public ActionResult CreateCommentAsync(CommentCreateViewModel model)
{
if (!model.UserId.HasValue)
return Content("false");

var svc = DependencyResolver.Current.GetService<IPartnerUserService>();
var user = svc.FindBy(model.UserId.Value, UserContext.Current.ActiveUser);

// I put this in here as a test -- it throws the no-session error, too.
var count = user.Comments.Count();

var comment = new Comment();
comment.CommentText = model.CommentText;
comment.DateCreated = DateTime.UtcNow;
comment.CreatedBy = UserContext.Current.ActiveUser;

// This is the original source of the error
user.Comments.Add(comment);
svc.Save(user, UserContext.Current.ActiveUser);

return Content("true");
}

我已经调试了应用程序,并确认在请求的开头创建了一个 session ,并且最令人困惑的是,即使我遇到上述错误的断点, SessionFactory.GetCurrentSession().IsOpen也是如此。

此外,当我渲染显示评论列表的 View 时,会填充 Comments列表。添加它时,我不知道为什么它会失败。

好像还不够,每隔一段时间不更改代码,我就不会收到错误,可以成功添加注释。我正在拔头发...有什么想法吗?这当然是一个 session 管理问题,但是我已经遍历了所有可以在网上找到的所有内容,并且通过各种方式可以进行 session 管理。有任何想法吗?

更新:
我尝试了一些其他测试,尤其是当前 session 是否具有我要操纵的用户对象。是的当我这样测试时:
if (!SessionFactory.GetCurrentSession().Contains(user))
SessionFactory.GetCurrentSession().Refresh(user);

我在条件上得到了 true的结果。

评论者请求了服务上的代码,但是对于该特定 call ,它不涉及 session ,它只是验证发出请求的用户是否具有权限,然后设置分离的条件。然后在该服务中调用该存储库,以下是该代码:
public IEnumerable<T> FindBy(DetachedCriteria detachedCriteria) //Infrastructure.Querying.Query query)
{
return detachedCriteria.GetExecutableCriteria(SessionFactory.GetCurrentSession()).Future<T>();
}

我不认为此代码是问题的原因是,它与为Details View 调用的代码完全相同。显示注释时,我没有任何延迟加载错误,并且以相同的方式进行操作-我使用该服务加载用户对象,然后在列表中进行 foreach迭代。我从来没有遇到任何问题。

万一这是AJAX调用的问题,我也将其更改为完整的回发,但仍然遇到相同的错误。

我一辈子都无法弄清楚这里发生了什么。

最佳答案

我终于发现了此错误的原因,但只能靠运气。我会发布解决方案,以防它对某人有所帮助,但是我真的无法提供任何解释为什么这样做,并且可能会发布一个新问题以查看某人是否可以解决问题。

您会在我的代码中注意到我正在这样调用我的服务:

var user = svc.FindBy(model.UserId.Value, UserContext.Current.ActiveUser);

UserContext对象是一个静态类,带有一个 session 存储的 Current实例,该实例包含一个属性,该属性是当前用户记录的NHibernate对象。由于NHibernate代理属性的方式,每次调用 UserContext.ActiveUser属性都必须执行以下操作:
if (!SessionFactory.GetCurrentSession().Contains(ActiveUser))
SessionFactory.GetCurrentSession().Refresh(ActiveUser);

出于某种原因,正是这种刷新过程使事情搞砸了。当我显式检索 Activity 用户而不是使用 UserContext类时,一切正常。

从那以后,我更改了检索 Activity 用户的方式,使其不使用 session 存储实例,并且工作正常。我希望我确切知道为什么,但是我不知道!

关于asp.net-mvc - NHibernate/MVC延迟加载因“session closed or no session”而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6194408/

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