创建 SessionFactory 时使用了无效或不完整的配置

发布于 2024-10-03 01:33:49 字数 4194 浏览 3 评论 0原文

这是最后的内部异常:

无法加载文件或程序集“ByteCode.Castle”或其依赖项之一。系统找不到指定的文件。

我添加了 nhibernate 的所有引用,这里使用的所有构建都是我的代码:

using NHibernate; 使用 FluentNHibernate; 使用 NHibernate.Cfg; 使用系统反射; 使用 FluentNHibernate.Cfg.Db; 使用 FluentNHibernate.Cfg; 使用 NHibernate.ByteCode.Castle; 使用 Castle.Core; 使用 Castle.DynamicProxy;

命名空间_3adaseh { 公共静态类 NHibernateHelper { 私有静态无效 ReferByteCode() { //只是为了确保 ByteCodeCastle 已加载 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
}

到目前为止

东西,我真的对这个错误感到厌倦,我向我的所有类库添加了 nhibernate 引用,但没有任何内容被修复,任何人都可以帮忙吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经大条 2024-10-10 01:33:51

确保您具有对 NHibernate.ByteCode.Castle.dll 和 Castle.Core.dll(以及 Castle.DynamicProxy2.dll,如果您使用 NH2.1.2)* 的程序集引用,以确保将其复制到输出目录中。您使用的是 Fluent NHibernate 和 NHibernate 的哪个版本?

* Castle.DynamicProxy2.dll 已与 Castle.Core.dll 合并。 NH3 中使用了 Castle.Core.dll 的较新合并版本。

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.

魂归处 2024-10-10 01:33:51

是的,正如詹姆斯所说。由于代理工厂仅在 NHibernate 的配置中指定,并且解决方案中的任何类库实际上并未使用它,因此它不会(总是)在构建时复制到应用程序项目。

对我来说有效的是仅引用所有类库中的最低限度,然后直接在应用程序项目中引用所有额外的部分。当我尝试运行应用程序项目时,它会告诉我缺少什么。像代理工厂(NHibernate.ByteCode.Castle、Castle.Core 和 Castle.DynamicProxy)、二级缓存(NHibernate.Caches.SysCache)和 HQL 解析(Antlr3.Runtime)等。只需将它们添加为引用即可每次投诉后的申请项目。

编辑

在您发布的错误中,它抱怨找不到“ByteCode.Castle”。有问题的程序集实际上是“NHibernate.ByteCode.Castle”。该错误可能出现在您定义代理工厂的 App.config 或 Web.config 中。您输入的程序集名称正确吗?

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

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>
来世叙缘 2024-10-10 01:33:51

好吧,问题是,在我的代码中,我正在编写

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

当我阅读 nhibernate 2.1 时,没有单词 nhibernate 从引用中删除了该单词,因此它正在搜索 bytecode.castle ,当我重命名该 dll 时,它发生了不匹配,我自己造成了一个大混乱,现在我刚刚删除了命名 nhibernate 并手动添加引用......并且它正在手动工作,谢谢大家:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文