SerializationException and Obfuscation

SerializationException and Obfuscation

Old forum URL: forums.lhotka.net/forums/t/4091.aspx


maxvor posted on Wednesday, December 26, 2007

Hi! I have a problem with such issue. I have collection class wich is inherited from BusinessListBase. In this class I've added some fields wich is used for remember the state and order of elements in the collection. One of the field has Stack type, where UserActions is an enumeration. And one more field representing Snapshot of the current state. All works fine until application is not obfuscated. But when it is method Clone of BusinessListBase fails with SerializationException on the line formatter.Deserialize(memoryStream) (something like this). I've tried to include source code of CSLA in my project to ensure that all the methods are in the same assembly but it didn't help. Also I tried to use [ObfuscationAttribute(Exclude = true)] for collection class but is also did nothing. Please can anyone give some help because google couln't help :) Maybe some problem with serialization of private fields? But why than without obfuscation all works fine?

Some details:

    /// <summary>
    /// Enumeration describing possible edit actions in the virtual list
    /// </summary>
    [Serializable()]
    [ObfuscationAttribute(Exclude = true)]
    public enum VirtualListEditActions
    {
        ElementsUp,
        ElementsDown,
        ElementEdit
    }

    [Serializable()]
    [ObfuscationAttribute(Exclude = true)]
    public class ElementsVirtualList : BusinessListBase<ElementsVirtualList, VirtualElement>
    {
        private Stack<VirtualListEditActions> _actionsStack = new Stack<VirtualListEditActions>();
        private Stack<List<int>> _snapshotsStack = new Stack<List<int>>();

        /// <summary>
        /// Gets the actions stack.
        /// </summary>
        /// <value>The actions stack.</value>
        public Stack<VirtualListEditActions> ActionsStack
        {
            get { return _actionsStack; }
        }


        /// <summary>
        /// Saves the state of the list.
        /// </summary>
        /// <param name="action">The edit action.</param>
        public void SaveState(VirtualListEditActions action)
        {
            if (action != VirtualListEditActions.ElementEdit)
            {
                List<int> snapshot = new List<int>();
                foreach (VirtualElement element in this)
                    snapshot.Add(element.Id);
                _snapshotsStack.Push(snapshot);
            }
            _actionsStack.Push(action);
        }


        /// <summary>
        /// Restores the state of the list.
        /// </summary>
        public void RestoreState()
        {
            try
            {
                VirtualListEditActions action = _actionsStack.Pop();
                switch (action)
                {
                    case VirtualListEditActions.ElementEdit:
                        this.CancelEdit();
                        break;
                    case VirtualListEditActions.ElementsUp:
                    case VirtualListEditActions.ElementsDown:
                        RestoreSnapshot(action);
                        break;
                    default:
                        break;
                }
            }
            catch { }
        }


        /// <summary>
        /// Restores the snapshot.
        /// </summary>
        /// <param name="action">The action wich was made last.</param>
        private void RestoreSnapshot(VirtualListEditActions action)
        {
            try
            {
                List<int> snapshot = _snapshotsStack.Pop();
                for (int i = 0; i < this.Count; i++)
                {
                    if (snapshotIdea [I] != thisIdea [I].Id)
                        for (int k = this.IndexOf(snapshotIdea [I]); k > i; k--)
                            this.ElementUp(k);
                }
            }
            catch {}
        }


        /// <summary>
        /// Get the index of element by id.
        /// </summary>
        /// <param name="elementId">The element id.</param>
        /// <returns></returns>
        private int IndexOf(int elementId)
        {
            for (int i = 0; i < this.Count; i++)
                if (thisIdea [I].Id == elementId)
                    return i;
            return -1;
        }


        /// <summary>
        /// Determines whether list contains the element with specified name.
        /// </summary>
        /// <param name="elementNm">The element name.</param>
        /// <returns>
        ///     <c>true</c> if contains the specified element name; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string elementNm)
        {
            foreach (VirtualElement element in this)
            {
                if (element.ElementName == elementNm)
                    return true;
            }
            return false;
        }


        /// <summary>
        /// Get the index of element by name.
        /// </summary>
        /// <param name="elementNm">The element name.</param>
        /// <returns></returns>
        public int IndexOf(string elementNm)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (thisIdea [I].ElementName == elementNm)
                     return i;
            }
            return -1;
        }
    }

************** Exception Text **************
System.Runtime.Serialization.SerializationException: Unable to find assembly 'DataBrowser, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at Csla.Serialization.BinaryFormatterWrapper.Deserialize(Stream serializationStream)
   at Csla.Core.ObjectCloner.Clone(Object obj)
   at Csla.BusinessListBase`2.GetClone()
   at Csla.BusinessListBase`2.Clone()
   at WipLab.DataBrowser.UI.BrowseForm.CreateCopyVirtualList(ElementsVirtualList list)
   at WipLab.DataBrowser.UI.BrowseForm..ctor(ElementsVirtualList list, DataFormatStringList formatStringList, Boolean isSelectedElements)
   at WipLab.DataBrowser.UI.DataBrowserForm.Browse_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
MC
    Assembly Version: 3.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/mscorlib/2.0.0.0__b77a5c561934e089/mscorlib.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
—•œ‘”ˆ‰†‡—š–†ŒŒ“’š“
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/mscorlib/2.0.0.0__b77a5c561934e089/mscorlib.dll
----------------------------------------
License
    Assembly Version: 1.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/mscorlib/2.0.0.0__b77a5c561934e089/mscorlib.dll
----------------------------------------
DataBrowser
    Assembly Version: 1.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/mscorlib/2.0.0.0__b77a5c561934e089/mscorlib.dll
----------------------------------------
DevExpress.Utils.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.Utils.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.Utils.v7.3.dll
----------------------------------------
DevExpress.XtraEditors.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.XtraEditors.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.XtraEditors.v7.3.dll
----------------------------------------
DevExpress.Data.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.Data.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.Data.v7.3.dll
----------------------------------------
DevExpress.XtraRichTextEdit.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.XtraRichTextEdit.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.XtraRichTextEdit.v7.3.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.EnterpriseServices
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Data
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Transactions
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
DevExpress.XtraBars.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.XtraBars.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.XtraBars.v7.3.dll
----------------------------------------
System.Web
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Web/2.0.0.0__b03f5f7f11d50a3a/System.Web.dll
----------------------------------------
DevExpress.XtraGrid.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.XtraGrid.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.XtraGrid.v7.3.dll
----------------------------------------
DevExpress.XtraNavBar.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.XtraNavBar.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.XtraNavBar.v7.3.dll
----------------------------------------
DevExpress.XtraTreeList.v7.3
    Assembly Version: 7.3.2.0
    Win32 Version: 7.3.2.0
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/DevExpress.XtraTreeList.v7.3/7.3.2.0__9b171c9fd64da1d1/DevExpress.XtraTreeList.v7.3.dll
----------------------------------------
System.Design
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.832 (QFE.050727-8300)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Design/2.0.0.0__b03f5f7f11d50a3a/System.Design.dll
----------------------------------------
Accessibility
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------

Copyright (c) Marimer LLC