NHibernate 序列化问题 - 类型 'NHibernate.Cfg.Configuration' 未标记为可序列化

标签 nhibernate serialization

我正在尝试通过序列化 NHibernate.Cfg.configuration 对象(如 http://msdn.microsoft.com/en-us/magazine/ee819139.aspx 中所述)来缩短使用 NHibernate 的 C# Winforms 应用程序的启动时间。 )但是当我尝试序列化时出现以下错误:

System.Runtime.Serialization.SerializationException : Type 'NHibernate.Cfg.Configuration' in Assembly 'NHibernate, Version=1.2.1.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4' is not marked as serializable.

另一个提到这一点的 stackoverflow 帖子是 duplicate object graph and persist back as new in nhibernate但不幸的是,考虑到那里发布的评论,我仍然遇到同样的错误。

NHibernate.Cfg.Configuration 对象的 ClassMappings Collection 属性中包含的实体均不引用 NHibernate.Cfg.Configuration strong> 或 SessionSessionFactory。事实上,包含这些实体的项目与我的 NHibernateHelper 类所在的项目没有任何引用。

为了确保这一点,我还使用 [Serialized] 属性标记了所有类,但这也没有帮助。

下面是我的 NHibernateHelper 类和 NHibernate xml 配置:

NHibernateHelper:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;

namespace PurchaseOrder.Data
{
    /// <summary>
    /// A helper class which provides NHibernate session objects on demand.
    /// The class creates a session factory only the first time a client needs a new session.
    /// </summary>
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
        private const string SerializedConfiguration = "configurtion.serialized";

        public static ISessionFactory SessionFactory{
            get{
                if (_sessionFactory == null){
                    var configuration = LoadConfigurationFromFile();

                    if (configuration == null){
                        configuration = new Configuration();
                        configuration.Configure();
                        SaveConfigurationToFile(configuration);
                    }

                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

        private static Configuration LoadConfigurationFromFile(){
            try{
                using (var file = File.Open(SerializedConfiguration, FileMode.Open)){
                    var bf = new BinaryFormatter();
                    return bf.Deserialize(file) as Configuration;
                }
            }
            catch (Exception ex){
                return null;
            }
        }

        private static void SaveConfigurationToFile(Configuration configuration){
            using (var file = File.Open(SerializedConfiguration, FileMode.Create)){
                var bf = new BinaryFormatter();
                bf.Serialize(file, configuration);
            }
        }

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

        public static ISession GetCurrentSession(){
            return SessionFactory.GetCurrentSession();
        }
    }
}

NHibernate xml 配置:

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">Server=<ServerName>;initial catalog=<DBName>;Integrated Security=SSPI</property>
        <property name="show_sql">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="current_session_context_class">thread_static</property>
        <mapping assembly="<AssemblyName>" />
    </session-factory>
</hibernate-configuration>

有什么想法可能是什么问题吗?

谢谢

最佳答案

真正的问题是您使用的是 NHibernate 1.2。它真的很旧(现在已经有 3 岁了),它没有可序列化的配置 ( code here )。

升级到 NHibernate 2.1.2 或更好的 3.0。

关于NHibernate 序列化问题 - 类型 'NHibernate.Cfg.Configuration' 未标记为可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4203504/

相关文章:

c# - AutoMapper 和有条件地对子集合进行排序

c# - Nhibernate 无法删除具有空属性值的值对象

C++设计——网络数据包和序列化

.net - Json.NET 可以对流进行序列化/反序列化吗?

java - Java中多个类可以序列化同一个对象吗?

java - RMI 和普通对象序列化之间有区别吗?

c# - NHibernate 获取下一个生日

.net - 在 OnPreInsert、OnPreUpdate 中将对象添加到关联

c# - 使用 HBM 文件将字典映射到 NHibernate 中的子表

c# - 如何在C#中使用MessagePack?