asp.net 自定义角色提供程序 nhibernate 错误
您好,
我正在我的 nhibernate 应用程序中实现自定义角色提供程序 我有一个存储库,每当我想访问 nhibernate 会话时我都会调用它。
因此,当我的角色提供程序初始化自身时
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) {
base.Initialize(name, config);
Repository = new Repository();
}
,然后我重写
public override string[] GetRolesForUser(string username) {
var users = Repository.QueryAll<Users>();
//I then filter 等等,
}
但是当调用此函数时,我总是收到 NHibernate 会话已关闭的错误。 我调试了 nhibernate 源代码,结果发现这里的会话与我的控制器中的会话有不同的 guid(我也使用 ASP.NET MVC)。 当我到达这里时,这个特别的会议已经结束了。 不知道谁把它关了。我知道它是在应用程序启动时才启动的。
有谁知道我做错了什么? 我想仍然在此提供程序中使用 Nhibernate,但不再收到错误。 谢谢
HI,
I am implementing a custom role provider in my nhibernate application
I have a repository that I call whenever I want to access the nhibernate session.
So when my role provider initializes itself
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) {
base.Initialize(name, config);
Repository = new Repository();
}
Then I override
public override string[] GetRolesForUser(string username) {
var users = Repository.QueryAll<Users>();
//I then filter and so on
}
But when this function is called I always get an error that the NHibernate session is closed.
I debugged the nhibernate source code and it turns out that the session here has a different guid that the session in my controllers(I am using ASP.NET MVC also).
And this particular session is closed by the time I get here.
I don't know who closes it. I know it is started when the application starts and only then.
Does anyone have any idea what I am doing wrong?
I want to still use Nhibernate in this provider but not get the error any more.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了似乎完全相同的问题。不要忘记角色和成员资格提供程序仅实例化一次并在应用程序的整个生命周期中存在。如果您使用“每个 Web 请求会话”模式,则 ISession 将在第一个请求后关闭,然后对于后续请求,对提供程序内部的 ISession 的任何引用都可能为空。
您可以通过注入对 ISessionFactory 的引用并调用 GetCurrentSession 来解决此问题,而不是直接保存对 ISession 的引用。
I had what appears to be the exact same problem. Don't forget that Role and Membership providers are only instantiated once and exist for the lifetime of the application. If you're using a Session per Web request pattern, the ISession will be closed after the first request and then any reference to an ISession internal to the provider will likely be null for subsequent requests.
You can get around this by injecting a reference to the ISessionFactory and calling GetCurrentSession, instead of directly holding a reference to an ISession.
这就是我最终修复它的方法。
在我的存储库类中,我有这个:
我完全删除了构造函数
我把这个改为:
This is how I evetually fixed it.
in my repository class I had this:
I deleted the constructor entirely
I put in this instead: