Question

Considering this example as a base example, I created the application but when I execute this application I am getting the following error.

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: NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu Example: NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle

The following is the code snippet I am using.

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();    
    }
}

And my app.config file looks like

    <?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>

I have LinFu.DynamicProxy.dll instead of linfu.dll. Will it work? If not, then from where do I get this linfu.dll? Or is there any other solution?

Was it helpful?

Solution

Assuming you have NHibernate 2.1 Alpha3, copy LinFu.DynamicProxy.dll and NHibernate.ByteCode.LinFu.dll from \Required_For_LazyLoading\LinFu to your bin (or references)

Then your configuration line should work:

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

As an aside, I prefer the hibernate-configuration section block for configuration.

Edit: Here's the relevant sections from my web configuration if you wanted to set up with hibernate-configuration instead of key/value pairs.

Also, it's possible to just put the hibernate-configuration part in its own file called hibernate.cfg.xml. You can then use the xsd nhibernate-configuration.xsd that's in the download to validate your 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="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>

OTHER TIPS

We actually use Castle Proxy and have the following.

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

After that it's just a matter of making sure that ALL of the files in the NHibernate Castle lazy loading directory are in the bin.

LinFu.DynamicProxy.dll isn't enough. You also need NHibernate.ByteCode.Linfu.dll (and potentially others).

<?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>

Copy LinFu.DynamicProxy.dll and NHibernate.ByteCode.LinFu.dll to the NHibernate's folder and add the same DLL files to the project reference.

I got this error after publishing my project via Visual Studio 2008's right-click "Publish..." feature, when trying to push our MVC/NHibernate project out to our web server.

It turned out I just needed to set the correct options in the publish dialog. In particular, in the "Copy" section, specify "All files in the source project folder", and then it started working. "Only files needed to run this application" was not good enough, perhaps Visual Studio was not smart enough to figure out which DLLs were being lazy loaded?

rnate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top