繁体   English   中英

在ASP.NET 4.0中通过Web.config配置NHibernate

[英]Configuring NHibernate via Web.config in ASP.NET 4.0

所以我的单元测试是绿色的,是时候将这个闪亮的全新NHibernate驱动的DAL集成到我的网络应用程序中! 我真的不想维护两个配置文件,所以我已经将hibernate.cfg.xml迁移到我的Web.config文件中(即我将hibernate.cfg.xml的内容复制到我的Web.config中)。 这是我的Web.config中的相关位:

<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中,不应该在那里看吗? 我是否需要指出“嘿,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'哦。 我在博客上写了一篇完整的文章

尝试在最后调用.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();

还要确保在Web项目中引用了NHibernate.ByteCode.Castle.dll程序集。

事实证明我在配置部分使用了错误的类型。 您需要使用NHibernate的section处理程序,而不是通用的.NET处理程序。 我看到的行为是因为它全部加载在一个单例中。 首次访问时,配置将失败。 在随后的访问中,它会抛出奇怪的错误,因为配置最初失败了!

还有一点需要注意 - 我的博客上有完整的文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM