4

我们最近将我们的 windows 窗体 C# 项目从 NHibernate 2.0 升级到了 2.1。我们更新了 app.config 以包含“proxyfactory.factory_class”以指向所选代理(在我们的示例中为“NHibernate.ByteCode.Castle”)。升级后,程序按预期构建和运行,没有问题。我们的问题是,当尝试在 Visual Studio 2008 设计器中打开任何引用 NHibernate 的表单时,现在给我们以下错误(好像我们没有配置代理):

未配置 ProxyFactoryFactory。使用可用的 NHibernate.ByteCode 提供程序之一初始化会话工厂配置部分的“proxyfactory.factory_class”属性。

堆栈跟踪:

   at NHibernate.Bytecode.AbstractBytecodeProvider.get_ProxyFactoryFactory()
   at NHibernate.Cfg.Configuration.Validate()
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at DAL.NHibernateHelper..cctor() in ...\DAL\NHibernateHelper.cs:line 62

NHibernateHelper 的第 62 行:

    static NHibernateHelper()
    {
        var cfg = new Configuration();
        cfg.Configure();
        cfg.AddAssembly("DAL");
        sessionFactory = cfg.BuildSessionFactory(); // <-- line 62
    }

这是我们的 NHibernate 的 app.config 配置:

<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="connection.connection_string">Server=ourserver;initial catalog=ourdb;Integrated Security=SSPI</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>
</configuration>

有人对如何解决此问题有任何线索吗?谢谢!

4

4 回答 4

3

我刚刚解决了同样的错误。

我的数据访问程序集对 Nhibernate.ByteCode.Castle.Dll 有正确的引用,它出现在它的 bin/release 文件夹中。数据访问程序集中一切正常。

当我尝试使用测试程序集中的数据访问代码时引发错误。事实证明,即使我的测试程序集引用了数据访问项目,它也没有得到 Nhibernate.ByteCode.Castle.dll 的副本。在测试程序集中添加对 Nhibernate.ByteCode.Castle.dll 的引用解决了这个问题。

于 2009-08-07T19:45:45.183 回答
1

我有同样的问题。为了修复它,我不仅要显式引用 DLL,还要向测试 DLL 添加一个显式创建 NHibernate.ByteCode.Castle.ProxyFactoryFactory 的方法。

那成功了。

于 2009-11-05T17:39:27.483 回答
0

It seems that the Designer somehow initializes your SessionFactory. Have you tried to add the ByteCodeProvider dll as a reference to your Project?

[EDIT] Ok..., the fact that the Application runs when deployed implies that everything is configured correctly. So your problem is a VS DesignMode problem. Why (the heck) are you using NHibernate in designmode? If you really need it try setting the ByteCodeProvider in your code. If you don't need it and just want a quick workarround you could check the current VS mode with this Hack preventing Nhibernate from building the SessionFactory:

static NHibernateHelper()
{
    if(System.Diagnostics.Process.GetCurrentProcess().ProcessName != "devenv")
    {
        var cfg = new Configuration();
        cfg.Configure();
        cfg.AddAssembly("DAL");
        sessionFactory = cfg.BuildSessionFactory(); // <-- line 62
    }
}

If i were you i'd try to find the control or whatever needs this static NHibernateHelper and try to decouple.

于 2009-07-30T21:57:06.233 回答
0

确保在您的 Nhibernate.ByteCode.Castle 参考的属性中,将其设置为“始终复制”,以便将其复制到您的 \bin 文件夹。

于 2009-07-30T23:40:33.373 回答