11

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Meetu Choudhary
  • 1,373
  • 4
  • 14
  • 26
  • 1
    Probable duplicates: http://stackoverflow.com/questions/956281/nhibernate-proxyexception and http://stackoverflow.com/questions/969894/error-using-nhibernate – Stuart Childs Jun 10 '09 at 14:07
  • There is also a bug with the build target http://blog.frozzn.com/2010/03/nhibernatebytecodecastleproxyfactoryfac.html – cgreeno Mar 20 '10 at 02:10

5 Answers5

13

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben
  • 562
  • 3
  • 9
  • could you plese tell me what is the hibernate configuration and from where i will get the above dll's – Meetu Choudhary Jun 11 '09 at 02:30
  • 1
    You can download NHibernate 2.1 Beta1 from http://sourceforge.net/project/downloading.php?group_id=73818&filename=NHibernate-2.1.0.Beta1-bin.zip&a=5370114 – Ben Jun 11 '09 at 02:43
  • I have downloaded it still the same problem – Meetu Choudhary Jun 11 '09 at 07:45
  • 1
    Did you copy all dlls from required_bins and the two dlls from Required_For_LazyLoading/LinFu? Did you copy them to your bin directory or add a reference to them so they're automatically copied? Are you still using the key/value approach? (I can try it that way to see if I get your error too) – Ben Jun 11 '09 at 11:09
  • yes i downloaded all the dlls and tried both the approches but still the same error i don't know how to uploadf the code files here. else if i have uploded my project for your refrence – Meetu Choudhary Jun 11 '09 at 13:02
  • Thanks for your ID i have sent you the mail. i have attached the project as a zip file. please look into the matter – Meetu Choudhary Jun 12 '09 at 05:47
  • hey ben you haven't responded to my mail what happened? – Meetu Choudhary Jun 13 '09 at 13:15
  • Am looking at it now. Sorry, was out of town. – Ben Jun 15 '09 at 13:23
  • 1
    Sent you back an updated config that should work for you Config in web.config not app.config. And, web site seems to work differently with hibernate.cfg.xml than Web App Project, so I removed that and just updated your web.config. – Ben Jun 15 '09 at 17:16
  • How can this be that I correctly created my config-file, copied all of the required DLLs, referenced the sharedLibs under the Reference Path section of my project properties and I still get the error about that ProxyFactoryFactory; only, and I mean ONLY when I don't specify Configure("./hibernate.cfg.xml").BuildSessionFactory(), since when I specify path to the config-file, it works fine? – Will Marcouiller Jan 12 '10 at 20:57
9

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shane Courtrille
  • 13,960
  • 22
  • 76
  • 113
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>

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GSerjo
  • 4,725
  • 1
  • 36
  • 55
1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ogre Psalm33
  • 21,366
  • 16
  • 74
  • 92
-2
rnate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131