简体   繁体   English

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

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

So my unit tests are green, time to integrate this shiny new NHibernate-driven DAL in to my web app! 所以我的单元测试是绿色的,是时候将这个闪亮的全新NHibernate驱动的DAL集成到我的网络应用程序中! I don't really want to maintain two configuration files so I've migrated hibernate.cfg.xml in to my Web.config file (ie I copypasta'd the contents of hibernate.cfg.xml in to my Web.config). 我真的不想维护两个配置文件,所以我已经将hibernate.cfg.xml迁移到我的Web.config文件中(即我将hibernate.cfg.xml的内容复制到我的Web.config中)。 Here is the relevant bits from my 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>

In Global.asax, on Application_Start, I try to initialize my configuration: 在Global.asax中,在Application_Start上,我尝试初始化我的配置:

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

    RegisterRoutes(RouteTable.Routes);

    SessionProvider.Initialize();
}

All this really does is call new Configuration().Configure().AddAssembly("MyProject"); 所有这一切确实是调用new Configuration().Configure().AddAssembly("MyProject"); in accordance with the configuration code above. 按照上面的配置代码。

Interesting result: When I first hit the page, an exception is thrown: 有趣的结果:当我第一次点击页面时,会抛出异常:

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

Well, I put the configuration in Web.config, shouldn't it be lookign there? 好吧,我把配置放在Web.config中,不应该在那里看吗? Do I need to indicate "hey, NHibernate, pay attention -- the config data is in Web.config, dummy!" 我是否需要指出“嘿,NHibernate,注意 - 配置数据在Web.config中,虚拟!” anywhere? 地方?

When I then hit F5, the page comes up. 当我点击F5时,页面出现了。 Hurray! 欢呼! Now I try to do something with data access and I get this exception: 现在我尝试用数据访问做一些事情,我得到了这个例外:

[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>]

Huh, that's kinda weird too -- this worked just fine in test with configuration in hibernate.cfg.xml...and I am specifying this property in my Web.config...I wonder what could possibly be up? 嗯,这有点奇怪 - 这在hibernate.cfg.xml中的配置测试中运行得很好......我在我的Web.config中指定了这个属性......我想知道什么可能出现?

So, anyone have any ideas? 那么,任何人都有任何想法? Any help in solving this mystery would be super! 解决这个谜团的任何帮助都是超级的!

*Update: I found the issue. *更新:我发现了这个问题。 It looks like I wasn't using the correct type in my configs section! 看起来我的配置部分没有使用正确的类型! D'oh. D'哦。 I have a complete write up on my blog . 我在博客上写了一篇完整的文章

Try calling the .Configure() method at the end: 尝试在最后调用.Configure()方法:

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

Or if you prefer put it into the web.config: 或者如果你喜欢把它放到web.config中:

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

and then: 然后:

new Configuration().Configure();

Also make sure that the NHibernate.ByteCode.Castle.dll assembly is referenced in your web project. 还要确保在Web项目中引用了NHibernate.ByteCode.Castle.dll程序集。

It turns out that I was using the wrong type in the configuration section. 事实证明我在配置部分使用了错误的类型。 You need to use NHibernate's section handler, not the generic .NET one. 您需要使用NHibernate的section处理程序,而不是通用的.NET处理程序。 The behavior I was seeing was because it was all loaded in a singleton. 我看到的行为是因为它全部加载在一个单例中。 On first visit, the configuration would fail. 首次访问时,配置将失败。 On subsequent visits it would just throw weird errors because the configuration failed originally! 在随后的访问中,它会抛出奇怪的错误,因为配置最初失败了!

There is one other caveat -- I have a complete writeup on my blog . 还有一点需要注意 - 我的博客上有完整的文章

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

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