Bug using a Serialization Surrogate
Using also a very simple implementation of Serialization Surrogate the deserialization fail, if there is a forward reference, with a SerializationException and a message:
"The object with ID 1 was referenced in a fixup but does not exist."
this is a sample to see the bug:
Can anyone tell me if I'm wrong or confirm the error.
Also any workaround are welcome.
------------using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Binary; using System.Reflection; namespace SerializationTest { static class Program { [STAThread] static void Main() { MyClass ci = new MyClass(); ci.Name = "test1"; ci.RelatedClass = ci; MemoryStream stream = new MemoryStream(); Save(ci, stream); ci = null; stream.Position = 0; ci = Load(stream); // This throw a SerializationException Console.WriteLine(ci.Name); stream.Close(); } static void Save(MyClass app, Stream stream) { IFormatter myFrmt = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); ss.AddSurrogate(typeof(MyClass), myFrmt.Context, new MySerializationSurrogate()); myFrmt.SurrogateSelector = ss; myFrmt.Serialize(stream, app); } static MyClass Load(Stream stream) { IFormatter myFrmt = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); ss.AddSurrogate(typeof(MyClass), myFrmt.Context, new MySerializationSurrogate()); myFrmt.SurrogateSelector = ss; MyClass app = (MyClass)myFrmt.Deserialize(stream); return app; } } [Serializable] class MyClass { public MyClass RelatedClass; public string Name; } public class MySerializationSurrogate : ISerializationSurrogate { public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) { MyClass myClass = (MyClass)obj; info.AddValue("Name", myClass.Name); info.AddValue("RelatedClass", myClass.RelatedClass); } public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { MyClass myClass = (MyClass)obj; myClass.Name = info.GetString("Name"); myClass.RelatedClass = (MyClass)info.GetValue("RelatedClass", typeof(MyClass)); return myClass; } } }Mauro Sturaro
All Replies
Just checked it and it works fine on 1.1, though I removed "static" keyword of the definition of Program class (it's not supported in C# 1.0).
Yes Igor, I've test it now and with .NET 1.1 works well.
It seem a bug of .NET 2.0 Serialization only.
... but I must work with .NET 2.0 !
Has anyone heard any more on this issue ?
I am also experiencing the same problem and cannot think of any workaround.
At last I've found a confirmation from microsoft that this is a bug:
Please vote this bug if you have the same problem.
- I see that the feedback form says that it is resolved. Has this bug been resolved? If so, how do I get a fix? If not, is there a workaround?


