11

将此示例视为基本示例,我创建了应用程序,但是当我执行此应用程序时,出现以下错误。

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

以下是我正在使用的代码片段。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using NHibernate.Cfg;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Configuration cfg = new Configuration();
        cfg.AddAssembly("NHibernate");

        ISessionFactory factory = cfg.BuildSessionFactory();
        ISession session = factory.OpenSession();
        ITransaction transaction = session.BeginTransaction();
        User newUser = new User();
        newUser.Id = "joe_cool";
        newUser.UserName = "Joseph Cool";
        newUser.Password = "abc123";
        newUser.EmailAddress = "joe@cool.com";
        newUser.LastLogon = DateTime.Now;

        // Tell NHibernate that this object should be saved
        session.Save(newUser);

        // commit all of the changes to the DB and close the ISession
        transaction.Commit();
        session.Close();    
    }
}

我的app.config文件看起来像

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section
          name="nhibernate"
          type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        />
      </configSections>

      <nhibernate>
        <add
          key="hibernate.connection.provider"
          value="NHibernate.Connection.DriverConnectionProvider"
        />
        <add
          key="hibernate.dialect"
          value="NHibernate.Dialect.MsSql2000Dialect"
        />
        <add
          key="hibernate.connection.driver_class"
          value="NHibernate.Driver.SqlClientDriver"
        />
        <add
          key="hibernate.connection.connection_string"
          value="Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI"
        />
        <!--<add value="nhibernate.bytecode.castle.proxyfactoryfactory, nhibernate.bytecode.castle" key="proxyfactory.factory_class" />-->
        <!--<property name="proxyfactory.factory_class">NHibernate.ByteCode.Linfu.ProxyFactoryFactory, NHibernate.ByteCode.Linfu</property>-->
<!-- I have tried both the lines but still getting the same error -->
      </nhibernate>
    </configuration>

我有LinFu.DynamicProxy.dll而不是linfu.dll. 它会起作用吗?如果没有,那么我从哪里得到这个linfu.dll?或者有没有其他解决方案?

4

5 回答 5

13

假设你有 NHibernate 2.1 Alpha3,复制LinFu.DynamicProxy.dllNHibernate.ByteCode.LinFu.dll\Required_For_LazyLoading\LinFu你的 bin(或引用)

然后你的配置行应该工作:

<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu" />

顺便说一句,我更喜欢使用hibernate-configurationsection 块进行配置。

编辑:如果您想设置hibernate-configuration而不是键/值对,这是我的网络配置中的相关部分。

此外,也可以将hibernate-configuration零件放在自己的名为hibernate.cfg.xml. 然后,您可以使用xsd nhibernate-configuration.xsd下载中的那个来验证您的配置。

<configSections>
    <section name="hibernate-configuration"   type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="default_schema">kennelfinder.dbo</property>
        <property name="connection.provider">
            NHibernate.Connection.DriverConnectionProvider
        </property>
        <property name="proxyfactory.factory_class">
            NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
        </property>
        <property name="connection.connection_string">{Your connection string}</property>
        <property name="show_sql">false</property>
        <property name="connection.driver_class">
            NHibernate.Driver.SqlClientDriver
        </property>
        <property name="connection.isolation">ReadCommitted</property>
        <property name="use_proxy_validator">true</property>
        <mapping assembly="KennelFinder"/>
    </session-factory>
</hibernate-configuration>
于 2009-06-11T00:12:23.493 回答
9

我们实际上使用 Castle Proxy 并具有以下内容。

<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

之后,只需确保 NHibernate Castle 延迟加载目录中的所有文件都在 bin 中。

LinFu.DynamicProxy.dll还不够。您还需要NHibernate.ByteCode.Linfu.dll(可能还有其他人)。

于 2009-06-10T19:56:44.057 回答
1
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
    </configSections>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
            <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
            <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider </property>
            <property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </property>
            <property name="connection.connection_string">Server=(local);database=HelloNHibernate;Integrated Security=SSPI;</property>
            <property name="show_sql">false</property>
            <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver </property>
            <property name="connection.isolation">ReadCommitted</property>
            <property name="use_proxy_validator">true</property>
        </session-factory>
    </hibernate-configuration>
</configuration>

复制LinFu.DynamicProxy.dllNHibernate.ByteCode.LinFu.dllNHibernate 的文件夹,并将相同的 DLL 文件添加到项目引用中。

于 2009-12-05T21:28:40.290 回答
1

当我尝试将 MVC/NHibernate 项目推送到我们的 Web 服务器时,通过 Visual Studio 2008 的右键单击“发布...”功能发布我的项目后出现此错误。

原来我只需要在发布对话框中设置正确的选项。特别是,在“复制”部分,指定“源项目文件夹中的所有文件”,然后它就开始工作了。“仅运行此应用程序所需的文件”还不够好,也许 Visual Studio 不够聪明,无法确定哪些 DLL 被延迟加载?

于 2009-12-09T14:05:01.590 回答
-2
rnate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
于 2009-12-17T21:31:47.307 回答