【发布时间】:2016-02-04 16:23:35
【问题描述】:
我正在尝试使用 C# MVC nHibernate 构建网站。一切正常,但是当我在多个浏览器中浏览我的网站时出现错误:
已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭它。
我已将我的许多服务绑定到一个会话 (ISession)。
//ContentService Bingings
Bind<IContentService>().To<ContentService>().InRequestScope();
Bind<ISession>()
.ToMethod(
context =>
context.Kernel.Get<IMasterSessionSource>()
.ExposeConfiguration()
.BuildSessionFactory()
.OpenSession()
)
.WhenInjectedInto<IContentService>()
.InSingletonScope();
//GeneralService Bindings
Bind<ISession>()
.ToMethod(
context =>
context.Kernel.Get<IMasterSessionSource>()
.ExposeConfiguration()
.BuildSessionFactory()
.OpenSession())
.WhenInjectedInto<IGeneralService>()
.InSingletonScope();
Bind<IGeneralService>()
.To<GeneralService>()
.InSingletonScope();
//CrossSellService Bindings
Bind<ISession>()
.ToMethod(
context =>
context.Kernel.Get<IMasterSessionSource>()
.ExposeConfiguration()
.BuildSessionFactory()
.OpenSession())
.WhenInjectedInto<ICrossSellService>()
.InSingletonScope();
Bind<ICrossSellService>()
.To<CrossSellService>()
.InSingletonScope();
//Details Bindings
Bind<ISession>()
.ToMethod(
context =>
context.Kernel.Get<IMasterSessionSource>()
.ExposeConfiguration()
.BuildSessionFactory()
.OpenSession())
.WhenInjectedInto<IDetailsService>()
.InRequestScope();
Bind<IDetailsService>()
.To<DetailsService>()
.InRequestScope();
他们被要求用于其他用途。例如我的 DetailsService:
公共接口IDetailsService
{
//在这里覆盖;
}
public class DetailsService : IDetailsService
{
private static IContentService Context { get; set; }
public DetailsService(IContentService context)
{
Context = context;
}
...
其中 IContentService 是我的主要查询类:
public interface IContentService
{
IQueryable<Source> Sources { get; }
}
public class ContentService : IContentService
{
private readonly ISession _session;
public ContentService(ISession session)
{
_session = session;
}
public IQueryable<Source> Sources
{
get { return _session.Query<Source>(); }
}
不知道为什么它不访问多个查询
【问题讨论】:
标签: c# asp.net-mvc nhibernate