none
BinaryFormatter.Deserialize(Stream serializationStream) throw SerializtionException when VS2017 deserializes the project serialized by VS2013 RRS feed

  • Question

  • Hi All:

    Recently we are going to upgrade our development IDE from VS 2013 to VS 2017, but I find a problem which I have not found a workaround.

    You can reproduce with the following sample code (in C#).

    Foo.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Security.Permissions;
    using System.Text;
    using System.Threading.Tasks;

    namespace TestBinarySerializer
    {
        [Serializable]
        public class Foo //: IDeserializationCallback //: ISerializable
        {
            //[OptionalField]
            //[NonSerialized]
            public Func<string> Del;

            public string Name { get; set; }

            //public int Age { get; set; }


            //[OnDeserialized]
            //[OnDeserializing]
            //private void SetDelDefault(StreamingContext context)
            //{
            //    Del = null;
            //}

            //[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter = true)]
            //public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
            //{
            //    info.AddValue(null, Del);
            //}

            //void IDeserializationCallback.OnDeserialization(Object sender)
            //{
            //    Del = null;
            //}
        }
    }


    Program.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Threading.Tasks;

    namespace TestBinarySerializer
    {
        class Program
        {
            static void Main(string[] args)
            {
                Func<string> a = (() => "a");
                Func<string> b = (() => "b");

                Foo foo = new Foo() { Name = "aa" };
                foo.Del = a;

                //WriteFoo(foo);

                Foo bar = ReadFoo();
                //Console.WriteLine(bar.Del());
                Console.WriteLine(bar.Name);

                Console.ReadKey();
            }

            public static void WriteFoo(Foo foo)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (var stream = new FileStream(@"C:/test.bin3", FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    formatter.Serialize(stream, foo);
                }
            }

            public static Foo ReadFoo()
            {
                Foo foo = null;
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
                formatter.FilterLevel = TypeFilterLevel.Low;

                using (var stream = new FileStream(@"C:/test.bin3", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    foo = formatter.Deserialize(stream) as Foo;
                }

                return foo;
            }
        }
    }


    Steps:

    1.Copy the source code to a new C# project created by VS 2013, build the solution, execute it to serialize the class Foo with the delegate Del.

    2.Open the solution by VS 2017, comment out the line

    Write Foo(foo); //comment out this line and rebuild again in VS 2017 

    rebuild the solution and execute again.

    3. Then an exception happens.

    Serialization Exception: 'Cannot get the member '<Main>b__1'.'



    Is there any solution or workaround? Help!!!

    I have tried a lot methods to fix it but failed, you can see it from the comments in the sample code.

    Friday, February 23, 2018 4:45 AM

Answers

  • 1.The test.bin3 file is generated by binary serialization with VS2013.

    2.Then open the same solution with VS2017, comment out the WriteFoo(foo) to avoid override the test.bin3 file.

    3.Rebuild the solution by VS2017.

    4.Run again to deserialize the file.

    • Marked as answer by Paddy_82 Thursday, March 1, 2018 3:08 AM
    Thursday, March 1, 2018 3:01 AM

All replies

  • Hi Paddy_82,

    Thanks for posting here.

    This forum is about MSBuild(Discuss the Microsoft build engine aka MSBuild. Topics including: usage of MSBuild, build customization, and MSBuild extensibility via tasks, loggers and hosting.), as you question is more related to C#, I will move it to the correct forum  for better support.

    If you have any MSBuild question, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.


    MSDN Community Support Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 23, 2018 6:45 AM
  • Hi Paddy_82,

    I test the code in Visual Studio 2013 and Visual Studio 2017.

    In Visual Studio 2017, I do not have the file test.bin3, I use .txt file to test. I could not reproduce the exception Serialization Exception: 'Cannot get the member '<Main>b__1'.' like yours.

    Regards of comment out or not Write Foo(foo);, when I rebuild the code, it is successful.

    Please provide more information about how you get the exception.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 1, 2018 2:48 AM
    Moderator
  • 1.The test.bin3 file is generated by binary serialization with VS2013.

    2.Then open the same solution with VS2017, comment out the WriteFoo(foo) to avoid override the test.bin3 file.

    3.Rebuild the solution by VS2017.

    4.Run again to deserialize the file.

    • Marked as answer by Paddy_82 Thursday, March 1, 2018 3:08 AM
    Thursday, March 1, 2018 3:01 AM
  • 1.The test.bin3 file is generated by binary serialization with VS2013.

    2.Then open the same solution with VS2017, comment out the WriteFoo(foo) to avoid override the test.bin3 file.

    3.Rebuild the solution by VS2017.

    4.Run again to deserialize the file.

    Yes, Firstly, the test.bin had to be generated with VS2013, Then use the VS2017 to deserialize this file. It will cause this exception. I have post this issue to Microsoft and it can be reproduced in the testing lib. And support engineer had sent the email to product group to find workaround. I am waiting the feedback. Thanks for all your input.
    Thursday, March 1, 2018 3:12 AM
  • Hi Paddy_82,

    If it is possible, could provide a simple test.bin3 for us to test? On my side, I could not reproduce the exception. The file may be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 1, 2018 6:22 AM
    Moderator