2008-12-11 17 views
3

我有在被在我的Application_Start方法中使用的Autofac模塊以下代碼:如何使用Autofac確保每個請求都有一個NHibernate ISession?

builder.Register(c => new Configuration().Configure().BuildSessionFactory()) 
    .SingletonScoped(); 
builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()) 
    .HttpRequestScoped(); 

builder.Register<NHibernateSomethingRepository>().As<ISomethingRepository>(); 

存儲庫的構造函數一個ISession作爲參數。但是我最終得到了整個應用程序的一個會話,儘管我明確要求它是HttpRequestScoped。

我已經配置了ContainerDisposal HTTP模塊。

根據documentation你必須創建一個嵌套的容器,但我讓Autofac autowire依賴關係。

我該怎麼辦?

謝謝!

回答

8

我發現這個問題,所以我會回答我自己的問題。

我註冊了默認作用域,它在Autofac中是單例作用域。我應該這樣做:

builder.Register<NHibernateSomethingRepository>() 
    .As<ISomethingRepository>() 
    .HttpRequestScoped; 
+1

注意:在Autofac2中,默認設置已經更改 - 現在是:'factoryScoped'AKA'InstancePerDependancy'在新的說法中。 – UpTheCreek 2010-05-08 15:30:29

相關問題