使用Nhibernate为常规存储库设置Ninject

使用Nhibernate为常规存储库设置Ninject,nhibernate,asp.net-web-api,ninject,Nhibernate,Asp.net Web Api,Ninject,我正在使用Nhibernate和通用存储库开发一个.NETWebAPI应用程序。现在,我正在尝试使用Ninject正确设置依赖项注入。但是,我当前的配置有一些问题:有时候,当我向DAL发出请求并尝试从数据库获取数据时,我的NHibernate ISession(在下面的UnitOfWork.cs中)对象要么为null,要么已经关闭 我还没有弄清楚为什么会发生这种情况,或者我的代码中有什么错误。我认为我的Ninject范围/绑定在某种程度上是不正确的,但无法使其工作 这是我当前的实现(我去掉了不相

我正在使用Nhibernate和通用存储库开发一个.NETWebAPI应用程序。现在,我正在尝试使用Ninject正确设置依赖项注入。但是,我当前的配置有一些问题:有时候,当我向DAL发出请求并尝试从数据库获取数据时,我的NHibernate ISession(在下面的UnitOfWork.cs中)对象要么为null,要么已经关闭

我还没有弄清楚为什么会发生这种情况,或者我的代码中有什么错误。我认为我的Ninject范围/绑定在某种程度上是不正确的,但无法使其工作

这是我当前的实现(我去掉了不相关的代码以减少显示的代码量):

NinjectWebCommon.cs

私有静态无效注册服务(IKernel内核)
{
UnitOfWorkFactory uow=新的UnitOfWorkFactory(
ConfigurationManager.ConnectionString[“foo”]。ConnectionString,
Assembly.getExecutionGassembly());
kernel.Bind();
kernel.Bind().ToMethod(f=>f.kernel.Get m.FluentMappings.AddFromAssembly(Assembly.getExecutionGassembly());
Configuration=cfg.BuildConfiguration();
SessionFactory=Configuration.BuildSessionFactory();
}
受保护的ISessionFactory会话工厂{get;private set;}
受保护的配置{get;private set;}
公共IUnitOfWork BeginUnitOfWork()
{
返回新的UnitOfWork(this.SessionFactory.OpenSession());
}
公共无效EndUnitOfWork(IUUnitOfWork unitOfWork)
{
var nhUnitOfWork=作为unitOfWork的unitOfWork;
if(unitOfWork!=null)
{
unitOfWork.Dispose();
unitOfWork=null;
}
}
公共空间处置()
{
if(this.SessionFactory!=null)
{
(此.SessionFactory为IDisposable).Dispose();
this.SessionFactory=null;
this.Configuration=null;
}
}
}
IUnitOfWork.cs

公共接口IUnitOfWork:IDisposable
{
TEntity GetSingle(表达式),其中TEntity:class;
}
UnitOfWork.cs

公共类UnitOfWork:IUnitOfWork
{
公共工作单位(NHiberante.ISession会议)
{
if(会话==null)
{
抛出新异常(“会话”);
}
这个会话=会话;
}
public NHiberante.ISession会话{get;private set;}
私有IQueryable集(),其中tenty:class
{
返回Session.Query();
}
公共tenty GetSingle(表达式),其中tenty:class
{
返回集().SingleOrDefault(表达式);
}
公共空间处置()
{
if(this.Session!=null)
{
(this.Session作为IDisposable).Dispose();
this.Session=null;
}
}
}
IRepository.cs

公共接口假定,其中tenty:class
{
TEntity GetSingle(表达式);
}
Repository.cs

公共类存储库:i存储,其中tenty:class
{
公共存储库(IUnitOfWork unitOfWork)
{
如果(unitOfWork==null)
{
抛出新的ArgumentNullException(“unitOfWork”);
}
this.UnitOfWork=UnitOfWork;
}
受保护的IUnitOfWork UnitOfWork{get;private set;}
公共虚拟TEntity GetSingle(表达式)
{
返回UnitOfWork.GetSingle(表达式);
}
}
iccustomerservice.cs

公共接口ICCustomerService
{
Customer GetCustomer(字符串id);
}
客户服务

公共类CustomerService:ICCustomerService
{
私人只读IRepository\u customerRepo;
公共客户服务(IRepository customerRepo)
{
_customerRepo=customerRepo;
}
公共客户GetCustomer(字符串id)
{
返回_customerRepo.GetSingle(l=>l.ID==ID);
}
}
CustomerController.cs

公共类CustomerController:ApicController
{
私人ICCustomerService(用户服务);;
公共CustomerController(ICCustomerService customerService)
{
_customerService=customerService;
}
公共字符串获取(字符串id)
{
var customer=\u customerService.GetCustomer(id);
返回客户名称;
}
}
概括来说:我提出了一个简单的GetCustomer请求。CustomerController被注入CustomerService实例。然后向CustomerService注入存储库实例,并向存储库本身注入UnitOfWork实现(由UnitOfWorkFactory类中的BeginUnitOfWork()方法创建)。还值得一提的是,请求首先被身份验证委托处理程序(用于基本身份验证)截获。此处理程序还使用CustomerService

当向API发出请求时,(通过REST客户机或cURL或其他方式),这最初是可行的,但偶尔(或在以后的某个请求中),我在尝试访问ISession对象时(为NULL),在数据层中会出现错误,我必须重新启动服务器以使其重新工作

我错过了什么明显的东西吗?有人能解释一下如何解决这个问题吗?谢谢

更新


我已经对此进行了进一步调试,发现我的UnitOfWork在每个请求中都被正确地实例化,因此得到了一个新的ISession。但在某些情况下,UoW Dispose()方法会被触发两次(由于根据堆栈跟踪进行了一些NHibernate缓存/修剪)。这就是内部会话对象为null的原因。一旦触发此异常,在所有后续请求中,Ninject显然会使用此空会话查找UnitOfWork的现有实例:/

会话对象未管理