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.

    Sunday, February 25, 2018 11:37 PM

Answers

  • One learns to not take serialization across .NET Frameworks for the very reasons you are now facing.

    • Marked as answer by Paddy_82 Monday, February 26, 2018 2:18 AM
    Sunday, February 25, 2018 11:42 PM
  • Hi DA924x:

    Thanks for your reply. But we didn't upgrade the .net framework version, keep it as 4.5.1, just change the compiler environment from vs2013 to vs2017.

    Thanks

    I wouldn't do that either. Obviously, it's a problem. It may be best to leave as is, but do new development using VS2017. It's too far of a jump between VS2013 to 17 and not expect issues.
    We have to upgrade the Visual Studio version due to our company policy. Thanks for your comments, I have submitted the tickets to Microsoft. Hope they can give us some solution. 
    • Marked as answer by Paddy_82 Monday, February 26, 2018 7:03 AM
    Monday, February 26, 2018 7:03 AM

All replies

  • One learns to not take serialization across .NET Frameworks for the very reasons you are now facing.

    • Marked as answer by Paddy_82 Monday, February 26, 2018 2:18 AM
    Sunday, February 25, 2018 11:42 PM
  • Hi DA924x:

    Thanks for your reply. But we didn't upgrade the .net framework version, keep it as 4.5.1, just change the compiler environment from vs2013 to vs2017.

    Thanks

    Monday, February 26, 2018 12:13 AM
  • Hi DA924x:

    Thanks for your reply. But we didn't upgrade the .net framework version, keep it as 4.5.1, just change the compiler environment from vs2013 to vs2017.

    Thanks

    I wouldn't do that either. Obviously, it's a problem. It may be best to leave as is, but do new development using VS2017. It's too far of a jump between VS2013 to 17 and not expect issues.
    Monday, February 26, 2018 6:04 AM
  • Hi DA924x:

    Thanks for your reply. But we didn't upgrade the .net framework version, keep it as 4.5.1, just change the compiler environment from vs2013 to vs2017.

    Thanks

    I wouldn't do that either. Obviously, it's a problem. It may be best to leave as is, but do new development using VS2017. It's too far of a jump between VS2013 to 17 and not expect issues.
    We have to upgrade the Visual Studio version due to our company policy. Thanks for your comments, I have submitted the tickets to Microsoft. Hope they can give us some solution. 
    • Marked as answer by Paddy_82 Monday, February 26, 2018 7:03 AM
    Monday, February 26, 2018 7:03 AM