2

I'm trying to improve the startup time of a C# Winforms application that uses NHibernate by serializing the NHibernate.Cfg.configuration object (as described at http://msdn.microsoft.com/en-us/magazine/ee819139.aspx) but i'm getting the following error when i try and serialize:

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.

Another stackoverflow post that mentions this is duplicate object graph and persist back as new in nhibernate but unfortunately, taking into account the comments posted there i am still getting the same error.

None of the entities that are included in the ClassMappings Collection property of NHibernate.Cfg.Configuration object have any reference to NHibernate.Cfg.Configuration or the Session or SessionFactory. In fact, the project in which those entities are included has no reference whatsoever to the project in which my NHibernateHelper class is found.

Just to make sure, i have also marked all classes with the [Serializable] attribute but that hasn't helped either.

Below is my NHibernateHelper class and the NHibernate xml configuration:

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

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

Any ideas what might be the issue?

Thanks

1 Answer 1

3

The real problem is you're using NHibernate 1.2. It's really old (3 years old now), it didn't have a serializable Configuration (code here).

Upgrade to NHibernate 2.1.2 or better yet 3.0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.