locked
Binary Formatter Serialize Error... RRS feed

  • Question

  • Hi All,

    I am creating a Component Using C#.Net, theme of this component is, Handling the Session management, as part of this, i have Dictionary<string, Object>. so all the session values are placed in this dictionary,

    i am trying to Serialize this dictionary with Memory Stream and Binary Formater and convert to Byte[] then, stored in DB.

    reverse process, Deserializing then convert to Dictionary object.

    while Serialize i am getting below error.

    Note: after successfull creation of component, i register the component in component services and called in classic asp page,

    Error Details

    Type 'System.__ComObject' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

    MyCode:

    byte[] buff = null;
                using (MemoryStream mStream = new MemoryStream())
                {
                    try
                    {
                        BinaryFormatter binFormat = new BinaryFormatter();
                        binFormat.Serialize(mStream, m_Session);   -> Here i am getting error.
                        buff = mStream.ToArray();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.EventLog.WriteEntry("SaveSessionData - Serialize", ex.Message + ex.StackTrace, EventLogEntryType.Error);
                        throw;
                    }
                }

    ASP Page Code

    dim testmsg
    set testmsg = Server.CreateObject("SessionManagement.sessionMng")

    dim con
        set con = Server.CreateObject("ADODB.Connection")
        con.open testmsg.ConnectionSTring
        testmsg("objconn") = con  -> Here i could like to Hold Connection Object in Session

     this ADODB.Connection object going to my Component Dictionary as a value. that time i am getting error while Serialize.

    Some one can help me ? Thanks in Advance

    ---Chandra Mohan

    Wednesday, October 23, 2013 12:46 PM

Answers

  • Hello CM,

    My short answer is I don't know of a way to make the Com+ object serializable. 

    I would approach this differently though.  I would create as new class that implements the serializable interface (ISerializable).  You only need to implement the two methods for serialising and de-serialising.  This class will save to and read from xml the information you need to instantiate a new com object.

    Hopefully your com objects can be instantiated without too much difficulty.  Let me know how you get on but I might not reply until Monday.  Cheers


    Jeff

    Friday, October 25, 2013 8:14 PM

All replies

  • Hello Chandra,

    The issue is in the content you are serializing and not your approach.  The following console app example illustrates that your code will work if you only use objects that are inherently serializable.  This will mean you will need to ensure all objects in your bespoke session dictionary are marked as serializable or implement the ISerializable interface.

    Hope this helps

    class Program
        {
            static void Main(string[] args)
            {
                var source = GetBinaryFormatterSample();
    
                var result = ToByte(source);
                var target = FromByte(result);
            }
    
            private static Dictionary<string, object> FromByte(byte[] buff)
            {
                Dictionary<string, object> result;
                using (MemoryStream mStream = new MemoryStream(buff))
                {
                    try
                    {
                        BinaryFormatter binFormat = new BinaryFormatter();
                        result = binFormat.Deserialize(mStream) as Dictionary<string, object>;                    
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.EventLog.WriteEntry("SaveSessionData - Serialize", ex.Message + ex.StackTrace, EventLogEntryType.Error);
                        throw;
                    }
                }
    
                return result;
            }
    
            private static Dictionary<string, object> GetBinaryFormatterSample()
            {
                return new Dictionary<string, object>()
                    {
                        { "test1", 1 },
                        { "test2", 111f },
                        { "test3", "value" },
                        { "test4", new SimpleClass { id=1, name = "name"} }
                    };
            }
    
            [Serializable]
            class SimpleClass
            {
                public int id { get; set; }
                public string name { get; set; }
            }
    
            private static byte[] ToByte(object payload)
            {
                byte[] buff = null;
                using (MemoryStream mStream = new MemoryStream())
                {
                    try
                    {
                        BinaryFormatter binFormat = new BinaryFormatter();
                        binFormat.Serialize(mStream, payload);   
                        buff = mStream.ToArray();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.EventLog.WriteEntry("SaveSessionData - Serialize", ex.Message + ex.StackTrace, EventLogEntryType.Error);
                        throw;
                    }
                }
    
                return buff;
            }
    
        }


    Jeff

    Wednesday, October 23, 2013 8:28 PM
  • The error probably means that you cannot serialize the ADODB.Connection objects, since they are not serializable. Such objects usually are not stored for later use. Maybe you can store to database something else. You can create ADODB.Connection each time you need it, based on connection string for example.

    Thursday, October 24, 2013 7:43 PM
  • Hi Jeff,

    Thank you so much for your reply,

    my question is, how we can implement ISerializable interface to built-in types, 

    my requirement like, need to store some component object on the session,i

    is there a way to store such a component object on the session (here in my session component) ?

    Thanks

    Chandra Mohan J

    Friday, October 25, 2013 10:31 AM
  • Hi,

    ADODB.Connection is for example only, my actual requirement is, need to store Component Object on the session.

    Thanks

    Chandra Mohan J

    Friday, October 25, 2013 10:33 AM
  • Hi Jeff,

    Clear me one thing, is there any possible to set Serializable to Com+ objects (Components)b

    because i am trying to store COM+ object on the session. how to i serialize the com+ object

    please help me

    Thanks

    CM

    Friday, October 25, 2013 11:02 AM
  • Hello CM,

    My short answer is I don't know of a way to make the Com+ object serializable. 

    I would approach this differently though.  I would create as new class that implements the serializable interface (ISerializable).  You only need to implement the two methods for serialising and de-serialising.  This class will save to and read from xml the information you need to instantiate a new com object.

    Hopefully your com objects can be instantiated without too much difficulty.  Let me know how you get on but I might not reply until Monday.  Cheers


    Jeff

    Friday, October 25, 2013 8:14 PM
  • facing same problem. i need to convert the COM object into stream. and i need to create MD5 HASH form that stream. but i am getting same error of 'cant serialize '. 

    ERROR: Type 'System.__ComObject' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

    My VERY simple method:

    public string getMD5ChecksumOfObject(object obj) // obj is Slide object(Powerpoint Slide)
            {
                using (var md5 = MD5.Create())
                {
                    MemoryStream stream = new MemoryStream();
                    IFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, obj);//produce error here 
                    return Encoding.Default.GetString(md5.ComputeHash(stream));
                }
            }


    Thursday, January 31, 2019 3:45 PM