5

and here is the inner exception at the end :

Could not load file or assembly 'ByteCode.Castle' or one of its dependencies. The system cannot find the file specified.

I am adding all references for nhibernate , used all the builds here is my code :

using NHibernate; using FluentNHibernate; using NHibernate.Cfg; using System.Reflection; using FluentNHibernate.Cfg.Db; using FluentNHibernate.Cfg; using NHibernate.ByteCode.Castle; using Castle.Core; using Castle.DynamicProxy;

namespace _3adaseh { public static class NHibernateHelper { private static void ReferByteCode() { //Just to make sure the ByteCodeCastle is loaded ProxyFactory fake = new ProxyFactory(); }

    #region Session
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                ReferByteCode();
                var configuration = new Configuration();
                #region Configuring Fluent NHibernate
                IPersistenceConfigurer persistenceConfigurer = MsSqlConfiguration.MsSql2008.ConnectionString("Data Source=.;Initial Catalog=3adaseh;Integrated Security=True").ShowSql().ProxyFactoryFactory("ByteCode.Castle.ProxyFactoryFactory, ByteCode.Castle");
                //
                // initialize nhibernate with persistance configurer properties 
                //Configuration cfg = persistenceConfigurer.ConfigureProperties(new Configuration());
                //var persistenceModel = new PersistenceModel();
                //persistenceModel.AddMappingsFromAssembly(Assembly.Load("3adaseh.Mappings"));
                //persistenceModel.Configure(cfg);
                try
                {
                    _sessionFactory = Fluently.Configure().Database(persistenceConfigurer).Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load("3adaseh.Mappings"))).BuildSessionFactory();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }

                //cfg.SetProperty(
                // add mappings definition to nhibernate configuration 
                //try
                //{
                //    var persistenceModel = new PersistenceModel();
                //    persistenceModel.AddMappingsFromAssembly(Assembly.Load("3adaseh.Mappings"));
                //    persistenceModel.Configure(cfg);
                //    _sessionFactory = configuration.BuildSessionFactory();
                //}
                //catch (System.Exception ex)
                //{
                //    throw ex;
                //}
                  #endregion




            }
            return _sessionFactory;
        }
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
    #endregion

    #region CRUD Operations
    public static void Add<T>(T newObject)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(newObject);
                transaction.Commit();
            }
        }
    }


    public static void Update<T>(T updatedObject)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(updatedObject);
                transaction.Commit();
            }
        }
    }

    public static void Remove<T>(T deletedObject)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(deletedObject);
                transaction.Commit();
            }
        }
    }

    public static T GetById<T>(int objectID)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                return session.Get<T>(objectID);
            }
        }
    }
    #endregion
}

}

I couldnt test anything so far , I am really getting bored of this error , I added nhibernate references to all my class libraries and nothing is being fixed , can anyone help please??

3 Answers 3

2

Make sure that you have assembly references to NHibernate.ByteCode.Castle.dll and Castle.Core.dll (and Castle.DynamicProxy2.dll if you're using NH2.1.2)* to ensure that it is copied into the output directory. Which version of Fluent NHibernate and NHibernate are you using?

* Castle.DynamicProxy2.dll was merged with Castle.Core.dll. The newer merged version of Castle.Core.dll is used in NH3.

6
  • Okay here is my structure there is Project.Mappings , Project.Entites , Project.UnitTesting , Project.Business ,
    – user510336
    Nov 17, 2010 at 16:03
  • I am running this in unit testing , nhibernate helper exists in business , I go to fluent hibernate website download and download fluent nhibernate and use all dlls in the downloaded file , here is the versions i used fluentnhibernate-binary-1.2.0.690 ,fluentnhibernate-binary-1.2.0.691 ..I am adding reference to those in almost all my projects , plus iesi collections n log4net
    – user510336
    Nov 17, 2010 at 16:04
  • using NHibernate; using FluentNHibernate; using NHibernate.Cfg; using System.Reflection; using FluentNHibernate.Cfg.Db; using FluentNHibernate.Cfg; using NHibernate.ByteCode.Castle; using Castle.Core; using Castle.DynamicProxy; , here is the using part in the nhibernate helper btw , here is what I do I got an external folder called references and I make all references through that folder , so when I download a new version I only clear this folder and put all the references inside it.
    – user510336
    Nov 17, 2010 at 16:06
  • Make sure that NHibernate.ByteCode.Castle.dll is actually in your output directory (e.g. bin\Debug) for your executable and/or tests. I've had problems with MsBuild deciding that NHibernate.ByteCode.Castle.dll isn't necessary for the EXE because it is referenced by a dependent assembly and not directly. Nov 17, 2010 at 17:12
  • I checked and it's being copied in each release folder where it's needed
    – user510336
    Nov 17, 2010 at 21:45
0

Yes, exactly what James said. Since the proxy factory is only specified in the config for NHibernate and not actually used by any of the class libraries in a solution, it doesn't (always) get copied to the application project on build.

What works for me is to only reference the bare minimum in all the class libraries, then reference all the extra bits and pieces directly in the application project. The application project will tell me what it is missing when I try to run it. Things like the proxy factory (NHibernate.ByteCode.Castle, Castle.Core and Castle.DynamicProxy), secondary cache (NHibernate.Caches.SysCache), and HQL parsing (Antlr3.Runtime), etc. Just keep adding them as references in the application project after each complaint.

Edit

In the error you've posted it's complaining about not finding 'ByteCode.Castle'. The assembly in question is actually 'NHibernate.ByteCode.Castle'. The error may be in your App.config or Web.config where you've defined the proxy factory. Have you typed the assembly name correctly?

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        ...
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
        ...
    </session-factory>
</hibernate-configuration>
2
  • how can i add them as references for the whole project??
    – user510336
    Nov 17, 2010 at 21:45
  • I am using unit testing and I dont want to have any configurations in app.config or web.config , I am using class library though(i can add app.config there?!?!? )
    – user510336
    Nov 18, 2010 at 0:02
0

Okay here was the problem , in my code I was writing

ProxyFactoryFactory("ByteCode.Castle.ProxyFactoryFactory, ByteCode.Castle");

without the word nhibernate as i read nhibernate 2.1 removed that word from references so it was searching for bytecode.castle , and when i was renaming that dll it was making a missmatch and i was creating a big mess by myself ,now i just removed the name nhibernate and added references manually.....and it's working manually , thanks all :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.