0

我的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))我试过从属性中注释掉InternalConfiguration
  4. 两种注册组件的方式都出现错误(一种在我的代码中注释)

我已经查看了关于 SO 的其他类似问题,但到目前为止没有任何帮助。

4

1 回答 1

0

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

  1. 创建会话并将其绑定到会话上下文的 BeforeRequest 钩子
  2. 提交会话的 AfterRequest 钩子
  3. 回滚的 OnError 钩子

代码如下:

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;
  }    
}
于 2013-04-08T10:47:48.863 回答