南希 - 无法解析类型:NHibernate.ISession

时间:2013-04-01 09:50:18

标签: nhibernate nancy

我的CustomBootstrapper如下所示

public class CustomBootstrapper : DefaultNancyBootstrapper
{
    protected override NancyInternalConfiguration InternalConfiguration
    {
        get
        {
            //This will tell Nancy it won't have to look in the Nhibernate assemblies for implementations of our interfaces.
            return NancyInternalConfiguration
                .Default
                .WithIgnoredAssembly(asm => asm.FullName.StartsWith("NHibernate", StringComparison.InvariantCulture))
                .WithIgnoredAssembly(asm => asm.FullName.StartsWith("Fluent", StringComparison.InvariantCulture))
                .WithIgnoredAssembly(asm => asm.FullName.StartsWith("Iesi", StringComparison.InvariantCulture));
        }
    }

    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
    {
        base.ConfigureRequestContainer(container, context);
        //container.Register((c, p) => SessionFactory.OpenSession());
        container.Register(SessionFactory.OpenSession());
    }
}

我有一个存储库,它接受ISession作为构造函数依赖项。当我运行我的应用程序时,出现错误Unable to resolve type: NHibernate.ISession

我可以确认

  1. 我的引导程序由Nanacy接收
  2. 执行ISession注册码
  3. 我尝试在.WithIgnoredAssembly(asm => asm.FullName.StartsWith("NHibernate", StringComparison.InvariantCulture))
  4. 财产中发表评论InternalConfiguration
  5. 我通过两种注册组件的方式得到错误(一个在我的代码中注释)
  6. 我已经查看了类似的其他问题,到目前为止还没有人帮助过。

1 个答案:

答案 0 :(得分:0)

我做了一个使用NH和Nancy的示例项目。在我设置的引导程序中:

  1. 创建会话并将其绑定到会话上下文的BeforeRequest挂钩
  2. 提交会话的AfterRequest挂钩
  3. 一个回滚的OnError挂钩
  4. 代码如下:

    public class Bootstrapper : WindsorNancyBootstrapper
    {
    
     // other stuff
    
     protected override void ApplicationStartup(IWindsorContainer container, IPipelines pipelines)
      {
        base.ApplicationStartup(container, pipelines);
    
        // other setup
    
        ConfigureNHibernateSessionPerRequest(container, pipelines);
      }
    
      private void ConfigureNHibernateSessionPerRequest(IWindsorContainer container, IPipelines pipelines)
      {
        pipelines.BeforeRequest += ctx => CreateSession(container);
        pipelines.AfterRequest += ctx => CommitSession(container);
        pipelines.OnError += (ctx, ex) => RollbackSession(container);
      }
    
      private Response RollbackSession(IWindsorContainer container)
      {
        var sessionFactory = container.Resolve<ISessionFactory>();
        if (CurrentSessionContext.HasBind(sessionFactory))
        {
          var requestSession = sessionFactory.GetCurrentSession();
          requestSession.Transaction.Rollback();
          CurrentSessionContext.Unbind(sessionFactory);
          requestSession.Dispose();
        }
        return null;
      }
    
      private Response CreateSession(IWindsorContainer container)
      {
        var sessionFactory = container.Resolve<ISessionFactory>();
        var requestSession = sessionFactory.OpenSession();
        CurrentSessionContext.Bind(requestSession);
        requestSession.BeginTransaction();
    
        return null;
      }
    
      private AfterPipeline CommitSession(IWindsorContainer container)
      {
        var sessionFactory = container.Resolve<ISessionFactory>();
        if (CurrentSessionContext.HasBind(sessionFactory))
        {
          var requestSession = sessionFactory.GetCurrentSession();
          requestSession.Transaction.Commit();
          CurrentSessionContext.Unbind(sessionFactory);
          requestSession.Dispose();
        }
        return null;
      }    
    }