views:

3711

answers:

4

We have recently upgraded our windows forms C# project from NHibernate 2.0 to 2.1. We updated our app.config to include the "proxyfactory.factory_class" to point to the chosen proxy ("NHibernate.ByteCode.Castle" in our case). After the upgrade the program builds and runs as expected, with no issues. Our problem is when attempting to open any forms that have references to NHibernate upon load within the Visual Studio 2008 designer, now give us the following error (as if we hadn't configured the proxy):

The ProxyFactoryFactory was not configured. Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.

Stack trace:

   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

Line 62 from NHibernateHelper:

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

Here is our app.config configuration for NHibernate:

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

Anyone have any clues on how to remedy this issue? Thanks!

A: 

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.

zoidbeck
NHibernate.ByteCode.Castle.dll (as well as other dependent dlls) have been referenced.
Jeremy Odle
We're not (at least intentionally) trying to use NHibernate in design mode. Simply opening a form in the designer triggers VS 2008 to try to initialize any objects associated with the form_load function (one of which being NHibernate). The line of code you provided looks like a viable option however. I'll give that a shot and post a reply on how that worked.
Jeremy Odle
Consider this as a Hack. I am not sure wheather this would also block SessionFactory creation in Debug mode. I think a better solution would be refactoring your NHibernateHelper. What about initializing the SessionFactory in Program.cs?
zoidbeck
A: 

Make sure that in the properties of your Nhibernate.ByteCode.Castle reference, you set it to "Copy Always" so that it gets copied to your \bin folder.

David P
There is no "Copy Always" option for the reference. However, Copy Local is set to True.
Jeremy Odle
+2  A: 

I just worked out the same error.

My data access assembly had a proper reference to Nhibernate.ByteCode.Castle.Dll and it appeared in its bin/release folder. Everything worked fine within the data access assembly.

The error was thrown when I tried to use data access code from my test assembly. It turned out that even though my test assembly referenced the data access project, it did not get a copy of the Nhibernate.ByteCode.Castle.dll. Adding a reference to Nhibernate.ByteCode.Castle.dll in the test assembly solved the problem.

bsk
I had same error in my project, adding reference to Nhibernate.ByteCode.Castle.dll solved my problem.
Ding
For those using Castle ActiveRecord and getting the same error, as well as adding the reference to NHibernate.ByteCode.Castle.dll, you'll need to add this to your XML configuration file: <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
Kyralessa
@Kyralessa. Thanks a million, I've been trying to get Castle ActiveRecord working, and you're comment fixed the issue I was having!
RichardOD
A: 

I had the same problem. To fix it, I had to not only explicitly reference the DLL, but add a method to the test DLL that explicitly created NHibernate.ByteCode.Castle.ProxyFactoryFactory.

That did the trick.

Julian Birch