2010-06-22 55 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