【问题标题】:Configuring NHibernate via Web.config in ASP.NET 4.0在 ASP.NET 4.0 中通过 Web.config 配置 NHibernate
【发布时间】:2011-03-06 16:01:03
【问题描述】:

所以我的单元测试是绿色的,是时候将这个闪亮的新 NHibernate 驱动的 DAL 集成到我的 Web 应用程序中了!我真的不想维护两个配置文件,所以我已将 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 中指定了这个属性......我想知道可能会发生什么?

那么,有人有什么想法吗?解开这个谜团的任何帮助都会非常棒!

*更新:我发现了问题。看起来我没有在我的配置部分使用正确的类型!哦。 I have a complete write up on my blog.

【问题讨论】:

    标签: c# nhibernate configuration web-config


    【解决方案1】:

    尝试在最后调用.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 程序集。

    【讨论】:

    • 谢谢达林。我按照您的建议做了-将我的程序集映射推送到配置文件(Web.config)并如上所述修改了我的配置方法。仍然没有喜悦(同样的错误)。另外,我确实在引用二进制文件并在本地复制它们。还有其他想法吗?
    【解决方案2】:

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

    还有一个警告——I have a complete writeup on my blog。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 2012-01-25
      • 2017-06-21
      相关资源
      最近更新 更多