gpt4 book ai didi

asp.net-mvc - 尝试根据 Web 请求实现 session ,没有配置当前 session 上下文

转载 作者:行者123 更新时间:2023-12-05 01:36:18 25 4
gpt4 key购买 nike

正如我在标题中所说的,我想为每个 Web 请求实现 session 。我的 session 提供程序是这样配置的(我对更改此配置不感兴趣。)

    public class SessionProvider
{
public static SessionProvider Instance { get; private set; }
private static ISessionFactory _SessionFactory;

static SessionProvider()
{
var provider = new SessionProvider();
provider.Initialize();
Instance = provider;
}

private SessionProvider()
{

}

private void Initialize()
{
string csStringName = "ConnectionString";
var cfg = Fluently.Configure()
....ommiting mappings and db conf.
.ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
.BuildConfiguration();
_SessionFactory = cfg.BuildSessionFactory();
}

public ISession OpenSession()
{
return _SessionFactory.OpenSession();
}

public ISession GetCurrentSession()
{
return _SessionFactory.GetCurrentSession();
}
}

在 Global.asax.cs 中,我有以下与每个 Web 请求的 session 相关的代码。

private static ISessionFactory SessionFactory { get; set; }

protected void Application_Start()
{
SessionFactory = MyDomain.SessionProvider.Instance.OpenSession().SessionFactory;

AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

}

protected void Application_BeginRequest(object sender, EventArgs e)
{
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
}

protected void Application_EndRequest(object sender, EventArgs e)
{
var session = CurrentSessionContext.Unbind(SessionFactory);
session.Dispose();
}

在调试我的 webapp 时我得到错误:没有配置当前 session 上下文。ErrorMessage 引用到 global.asax 行 CurrentSessionContext.Bind(session);

已更新添加了 .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))现在我收到关于从我的 Controller 中检索数据的错误消息错误信息: session 已关闭!对象名称:“ISession”。

Controller 代码:

using (ISession session = SessionProvider.Instance.GetCurrentSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
data = session.QueryOver<Object>()
...ommiting

transaction.Commit();
return PartialView("_Partial", data);
}

}

最佳答案

第一个问题

你需要在你的 nhibernate 配置部分配置它:

<property name="current_session_context_class">web</property>

我目前也在做这样的事情:

if (!CurrentSessionContext.HasBind(SessionFactory))
{
CurrentSessionContext.Bind(SessionFactory.OpenSession());
}

第二题

流畅修改配置请看下面文章:currentsessioncontext fluent nhibernate how to do it?

第三个问题

您两次关闭 session 。

using (ISession session = SessionProvider.Instance.GetCurrentSession()) 

关闭您的 session 。然后您在 Application_EndRequest 中再次执行此操作。如果您有其他问题,请发布新问题。

关于asp.net-mvc - 尝试根据 Web 请求实现 session ,没有配置当前 session 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690631/

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