views:

58

answers:

0

Hello

I'm having some problems with applying NHibernate Fluent Automapping. It worked great in a test project. But now..

Test method [PROJECTNAME].SubscriptionTest.SubscriptionConstructorTest threw exception:  NHibernate.MappingException: No persister for: [PROJECTLIB].SubscriptionManagerRP

The class (then again, the same exception arises with a much simpler testclass - so the problem shouldn't be here):

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://docs.oasis-open.org/wsn/b-2")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://docs.oasis-open.org/wsn/b-2", IsNullable = false)]
    [System.Runtime.Serialization.DataContractAttribute(Name = "SubscriptionManagerRP", Namespace = "http://docs.oasis-open.org/wsn/b-2")]
    public class SubscriptionManagerRP
    {

        private string id;

        public string Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public Boolean Save()
        {
            DatabaseAccess access = new DatabaseAccess();
            var sessionFactory = access.getSessionFactory();

            //try
            //{
                using (var session = sessionFactory.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {


                        SaveTextMess(this.ToString());
                        session.Save(this);
                        transaction.Commit();
                        return true;
                    }
                }
            //}
            //catch (Exception e) 
            //{
            //    SaveTextMess("ERROR: " + e);
            //    Console.WriteLine(e); 
            //}

            //SaveTextMess("false");
            return false;
        }

        private void SaveTextMess(String output)
        {
            //Just for Demo purposes, saving text file per message that should be sent

            // create a writer and open the file
            TextWriter tw = new StreamWriter("C:\\Temp\\CespSubscriptionManagerRPMessage.txt");

            // write a line of text to the file
            tw.WriteLine(output);

            // close the stream
            tw.Close();
        }

//###################################




        [EditorBrowsable(EditorBrowsableState.Never)]
        private EndpointReferenceType consumerReferenceField;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private FilterType filterField;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private SubscriptionPolicyType subscriptionPolicyField;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private System.DateTime creationTimeField;

        [EditorBrowsable(EditorBrowsableState.Never)]
        private bool creationTimeFieldSpecified;

        private static System.Xml.Serialization.XmlSerializer serializer;

        /// <summary>
        /// .ctor class constructor
        /// </summary>
        public SubscriptionManagerRP()
        {
            this.subscriptionPolicyField = new SubscriptionPolicyType();
            this.filterField = new FilterType();
            this.consumerReferenceField = new EndpointReferenceType();
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public EndpointReferenceType ConsumerReference
        {
            get
            {
                return this.consumerReferenceField;
            }
            set
            {
                this.consumerReferenceField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public FilterType Filter
        {
            get
            {
                return this.filterField;
            }
            set
            {
                this.filterField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public SubscriptionPolicyType SubscriptionPolicy
        {
            get
            {
                return this.subscriptionPolicyField;
            }
            set
            {
                this.subscriptionPolicyField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public System.DateTime CreationTime
        {
            get
            {
                return this.creationTimeField;
            }
            set
            {
                this.creationTimeField = value;
            }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        [System.Runtime.Serialization.DataMemberAttribute()]
        public bool CreationTimeSpecified
        {
            get
            {
                return this.creationTimeFieldSpecified;
            }
            set
            {
                this.creationTimeFieldSpecified = value;
            }
        }

        private static System.Xml.Serialization.XmlSerializer Serializer
        {
            get
            {
                if ((serializer == null))
                {
                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(SubscriptionManagerRP));
                }
                return serializer;
            }
        }

        #region Serialize/Deserialize
        /// <summary>
        /// Serializes current SubscriptionManagerRP object into an XML document
        /// </summary>
        // <returns>string XML value</returns>
        public virtual string WriteObject()
        {
            System.IO.StreamReader streamReader = null;
            System.IO.MemoryStream memoryStream = null;
            try
            {
                memoryStream = new System.IO.MemoryStream();
                Serializer.Serialize(memoryStream, this);
                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
                streamReader = new System.IO.StreamReader(memoryStream);
                return streamReader.ReadToEnd();
            }
            finally
            {
                if ((streamReader != null))
                {
                    streamReader.Dispose();
                }
                if ((memoryStream != null))
                {
                    memoryStream.Dispose();
                }
            }
        }

        /// <summary>
        /// Deserializes workflow markup into an SubscriptionManagerRP object
        /// </summary>
        // <param name="xml">string workflow markup to deserialize</param>
        // <param name="obj">Output SubscriptionManagerRP object</param>
        // <param name="exception">output Exception value if deserialize failed</param>
        // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool ReadObject(string xml, out SubscriptionManagerRP obj, out System.Exception exception)
        {
            exception = null;
            obj = default(SubscriptionManagerRP);
            try
            {
                obj = ReadObject(xml);
                return true;
            }
            catch (System.Exception ex)
            {
                exception = ex;
                return false;
            }
        }

        public static bool ReadObject(string xml, out SubscriptionManagerRP obj)
        {
            System.Exception exception = null;
            return ReadObject(xml, out obj, out exception);
        }

        public static SubscriptionManagerRP ReadObject(string xml)
        {
            System.IO.StringReader stringReader = null;
            try
            {
                stringReader = new System.IO.StringReader(xml);
                return ((SubscriptionManagerRP)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
            }
            finally
            {
                if ((stringReader != null))
                {
                    stringReader.Dispose();
                }
            }
        }

        /// <summary>
        /// Serializes current SubscriptionManagerRP object into file
        /// </summary>
        // <param name="fileName">full path of outupt xml file</param>
        // <param name="exception">output Exception value if failed</param>
        // <returns>true if can serialize and save into file; otherwise, false</returns>
        public virtual bool SaveToFile(string fileName, out System.Exception exception)
        {
            exception = null;
            try
            {
                SaveToFile(fileName);
                return true;
            }
            catch (System.Exception e)
            {
                exception = e;
                return false;
            }
        }

        public virtual void SaveToFile(string fileName)
        {
            System.IO.StreamWriter streamWriter = null;
            try
            {
                string xmlString = WriteObject();
                System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
                streamWriter = xmlFile.CreateText();
                streamWriter.WriteLine(xmlString);
                streamWriter.Close();
            }
            finally
            {
                if ((streamWriter != null))
                {
                    streamWriter.Dispose();
                }
            }
        }

        /// <summary>
        /// Deserializes workflow markup from file into an SubscriptionManagerRP object
        /// </summary>
        // <param name="xml">string workflow markup to deserialize</param>
        // <param name="obj">Output SubscriptionManagerRP object</param>
        // <param name="exception">output Exception value if deserialize failed</param>
        // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
        public static bool LoadFromFile(string fileName, out SubscriptionManagerRP obj, out System.Exception exception)
        {
            exception = null;
            obj = default(SubscriptionManagerRP);
            try
            {
                obj = LoadFromFile(fileName);
                return true;
            }
            catch (System.Exception ex)
            {
                exception = ex;
                return false;
            }
        }

        public static bool LoadFromFile(string fileName, out SubscriptionManagerRP obj)
        {
            System.Exception exception = null;
            return LoadFromFile(fileName, out obj, out exception);
        }

        public static SubscriptionManagerRP LoadFromFile(string fileName)
        {
            System.IO.FileStream file = null;
            System.IO.StreamReader sr = null;
            try
            {
                file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
                sr = new System.IO.StreamReader(file);
                string xmlString = sr.ReadToEnd();
                sr.Close();
                file.Close();
                return ReadObject(xmlString);
            }
            finally
            {
                if ((file != null))
                {
                    file.Dispose();
                }
                if ((sr != null))
                {
                    sr.Dispose();
                }
            }
        }
        #endregion

        #region Clone method
        /// <summary>
        /// Create a clone of this SubscriptionManagerRP object
        /// </summary>
        public virtual SubscriptionManagerRP Clone()
        {
            return ((SubscriptionManagerRP)(this.MemberwiseClone()));
        }
        #endregion
    }

The save method from the class above (looks the same in the simple testclass that works in the test project):

 public Boolean Save()
        {
            DatabaseAccess access = new DatabaseAccess();
            var sessionFactory = access.getSessionFactory();

            //try
            //{
                using (var session = sessionFactory.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {


                        SaveTextMess(this.ToString());
                        session.Save(this);
                        transaction.Commit();
                        return true;
                    }
                }
            //}
            //catch (Exception e) 
            //{
            //    SaveTextMess("ERROR: " + e);
            //    Console.WriteLine(e); 
            //}

            //SaveTextMess("false");
            return false;
        }

Where I set the connection string and the Session Factory:

 class SessionFactoryController
    {

        public SessionFactoryController()
        {

        }

        public ISessionFactory GiveFactory()
        {
            return CreateSessionFactory();
        }

        private static void ReferByteCode(){

            //Just to make sure the ByteCodeCastle is loaded
            ProxyFactory fake = new ProxyFactory();
        }

        private static ISessionFactory CreateSessionFactory()
        {
            ReferByteCode();

            var cfg = new FluentNhibernateConfiguration();

            return Fluently.Configure()
              .Database(
               FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005
                    .ConnectionString("[SERVER];Database=Pets;User ID=NHibernateTester;Password=[PASSWORD];Trusted_Connection=False;")
              )

              .Mappings(m => 
                  m.AutoMappings
                    .Add(AutoMap.AssemblyOf<SubscriptionManagerRP>(cfg))
                )

              .BuildSessionFactory();
        }

    }

Config:

class NotifyFluentNhibernateConfiguration : DefaultAutomappingConfiguration
    {
        public override bool ShouldMap(Type type)
        {
            return type.Namespace == "System.Xml.XmlAttribute";
        } 
    }

The config is to handle an earlier mapping exception for 'System.Xml.XmlAttribute'.

Is it possible to get this error if the database isn't set up correctly? I have done the test that with a class I know works with NHibernate Automapping (from another project) test towards the database (same as in the other project). And I still get the same exception, but for the 'simple' class.

So it isn't the class.

It isn't the connection string, because it was copied from the other project that works great.

It isn't the settings (or at least the security/access settings) on the database. I assume it should give another error if I configured the tables wrong, and as mentioned, I get the same exception when I direct it towards something that works in another project.

I have deleted everything and rewritten it once, just to make sure I didn't do some small silly misstake. If so, I've done it twice.

As mentioned, and as you see. This code is very, very simple. The only thing that is complex is the class. And even if I change it for a very, very simple class I get the same exception.

Any ideas please?