2010-06-22 56 views
4

所以我的單元測試是綠色的,有時候將這個閃亮的新NHibernate驅動的DAL集成到我的web應用程序中!我不想維護兩個配置文件,所以我已經將hibernate.cfg.xml遷移到了我的Web.config文件中(即,我將hibernate.cfg.xml的內容複製到了我的Web.config中)。下面是我的web.config中的相關位:在ASP.NET 4.0中通過Web.config配置NHibernate

<configSections> 
    <section name="combres" type="Combres.ConfigSectionSetting, Combres, Version=2.0.0.0, Culture=neutral, PublicKeyToken=49212d24adfbe4b4"/> 
    <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> 
</configSections> 

<nhibernate xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory name=""> 
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
    <property name="connection.connection_string">Data Source=(local)\SQLExpress;Initial Catalog=MyProject;Integrated Security=True</property> 
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> 
    <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 
    <listener class="MyProject.Sql.Listeners.SaveEventListener, MyProject" type="save"/> 
    <listener class="MyProject.Sql.Listeners.UpdateEventListener, MyProject" type="update"/> 
    </session-factory> 
</nhibernate> 

在Global.asax中,上的Application_Start,我嘗試初始化我的配置:

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 

    RegisterRoutes(RouteTable.Routes); 

    SessionProvider.Initialize(); 
} 

這一切確實是呼叫new Configuration().Configure().AddAssembly("MyProject");按照上面的配置代碼。

有趣的結果:當我第一次打到頁面,則拋出異常:

[FileNotFoundException: Could not find file 'D:\Build\MyProject\Source\MyProject.Web\bin\hibernate.cfg.xml'.] 

好吧,我把Web.config中的配置,應該不是在那裏lookign?我需要指出「嘿,NHibernate,注意 - 配置數據在Web.config中,虛擬!」地方?

當我點擊F5時,頁面出現。歡呼!現在,我嘗試做與數據訪問的東西,我得到這個異常:

[ProxyFactoryFactoryNotConfiguredException: The ProxyFactoryFactory was not configured. 
Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers. 
Example: 
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> 
Example: 
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>] 

咦,這是有點奇怪太 - 這工作就好了測試與hibernate.cfg.xml配置...和我在我的Web.config中指定這個屬性...我想知道什麼可能會起來?

那麼,任何人有任何想法?解決這個謎題的任何幫助都是超級的!

*更新:我找到了問題。它看起來像我沒有在我的配置部分使用正確的類型! D'哦。 I have a complete write up on my blog

回答

3

事實證明,我在配置部分使用了錯誤的類型。你需要使用NHibernate的section處理程序,而不是通用的.NET處理程序。我看到的行爲是因爲它是全部加載在一個單身人士。首次訪問時,配置將失敗。在隨後的訪問中,它只會拋出奇怪的錯誤,因爲配置最初失敗!還有一個警告 - I have a complete writeup on my blog

+0

非常有用。非常感謝neebur。 – Simian 2012-02-08 10:21:46

4

嘗試調用在年底.Configure()方法:

new Configuration().AddAssembly("MyProject").Configure(); 

或者如果你喜歡把它放進web.config中:

<nhibernate xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory name=""> 
    ...  
    <mapping assembly="MyProject" /> 
    </session-factory> 
</nhibernate> 

然後:

new Configuration().Configure(); 

而且確保NHibernate.ByteCode.Castle.dll程序集在您的Web項目中被引用。

+1

謝謝Darin。我按照你的建議做了 - 將我的程序集映射推送到配置文件(Web.config)並像上面那樣修改了我的配置方法。仍然沒有喜悅(相同的錯誤)。另外,我的確在引用二進制文件*和*在本地複製它們。任何其他想法? – 2010-06-22 15:20:42