2013-04-01 41 views
0

CustomBootstrapper看起來像下面南希 - 無法解析類型:NHibernate.ISession

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. 我得到錯誤與註冊組件的兩種方式(一個在我的代碼中註釋)

我看過類似的其他問題,所以迄今沒有任何幫助。

回答

0

我已經做了一個使用NH與南希的示例項目。在引導程序我設置:

  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; 
    }  
} 
+0

有了這個設計,你採取'ISessionFactory'無論你的依賴需要一個'ISession'? – Suhas

+0

是的。在例如存儲庫我在ISessionFactory上構造了一個依賴關係。 –

+0

這對我來說看起來有點奇怪。存儲庫不使用'ISessionFactory',因此它們不應該依賴於'ISessionFactory' – Suhas