动态更改连接字符串时发出 Rebind NInject
在连接字符串中,我应该通过使用连接字符串中的属性 application name=userid 动态传递logginguserid值,并使用查询select app_name()进入SQL Server。
使用的技术: 1).net 4.0 2)NHibernate 3)Ninject
在登录之前,我使用 Ninject IoC 容器和 NHibernate 加载没有应用程序名称属性的连接字符串,在登录之后,我将登录的用户 ID 作为构造函数值传递,并重新绑定 NhibernateConfiguration 类,如下
在登录之前 在注入 NHibernateConfiguration
public override void Load()
{
Bind<ISession>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>()
.GetSessionFactory()
.OpenSession());
Bind<ISessionFactory>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>().GetSessionFactory());
}
登录后,将logginguserid与构造函数参数一起传递,如下所示。
using (var kernal = ServiceLocator.GetKernel())
{
kernal.Rebind<NHibernateConfiguration>().To<NHibernateConfiguration>()
.WithConstructorArgument("loggedUserId", user.Id);
}
但我无法重新绑定或注入 NHibernateConfiguration 类。
请帮助我如何使用 Ninject 重新绑定 NHibernateConfiguration 类
In connection string i should dynamically passing the loggeduserid value by using the attribute application name=userid in connection string and getting into SQL Server by using the query select app_name().
Technologies used:
1).net 4.0
2)NHibernate
3)Ninject
Before logged-in i am using Ninject IoC container and NHibernate to load the connection string without the application name attribute and after logged in I am passing the logged user id as a constructor value and rebinding the NhibernateConfiguration class is as follows
Before Logged in inject the NHibernateConfiguration
public override void Load()
{
Bind<ISession>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>()
.GetSessionFactory()
.OpenSession());
Bind<ISessionFactory>().ToMethod(x => x.Kernel.Get<NHibernateConfiguration>().GetSessionFactory());
}
After logged in passing the loggeduserid with the constructor argument is as follows.
using (var kernal = ServiceLocator.GetKernel())
{
kernal.Rebind<NHibernateConfiguration>().To<NHibernateConfiguration>()
.WithConstructorArgument("loggedUserId", user.Id);
}
but I am not able rebind or inject the NHibernateConfiguration class .
Please help me how to rebind the NHibernateConfiguration class by using Ninject
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

不要在应用程序执行期间使用重新绑定。这可能会导致很多问题。使用条件出价代替:
此外,您的会话绑定没有多大意义。它应该改为获取会话工厂。
Don't use rebind during the execution of your application. This can cause many problems. Use conditional bidnings instead:
Also your session binding doesn't make much sense. It should get the session factory instead.