1

I'm using BinaryFormatter for serializing and deserializing custom file types. I have a stand alone application and a web application, which sould both read and write the same files. The standalone app is working fine, but when I read a file with my web app, an exception is thrown. The problem is I can't see exactly what is going on, how can I debug or fix this error ?

    BinaryFormatter b = new BinaryFormatter();
    b.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
    b.Binder = new WebBinder();

    object o = b.Deserialize(s); //Throws exception :

Object of type 'System.String' cannot be converted to type 'System.Decimal'.

    public class WebBinder : SerializationBinder
    {
        public override Type BindToType(string assemblyName, string typeName)
        {
            Type tyType = null;
            string sShortAssemblyName = assemblyName.Split(',')[0];
            Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly ayAssembly in ayAssemblies)
            {
                if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
                {
                    tyType = ayAssembly.GetType(typeName);
                    break;
                }
            }
            return tyType;
        }
    }

The same file deserializes fine in the standalone app??

Run CMD
  • 2,937
  • 3
  • 36
  • 61
  • The exception is telling you that you're trying to deserialize a Decimal but what was serialized was a String. – Peter Ritchie Jun 08 '12 at 12:54
  • 1
    Are you strictly tied to BinaryFormatter? A serializer that *isn't type dependent* could be a good idea – Marc Gravell Jun 08 '12 at 12:57
  • @MarcGravell: I'm curious, what did you mean by a *type dependent serializer*, is it in a sense that a t/d serializer depends on the *mem. size and layout* of the types or something else? – Boris B. Jun 08 '12 at 13:13
  • @MarcGravell: you're right, using Binary serialization was a bad, bad idea, especially in combination with obfuscation, but we're stuck with it now :-) – Run CMD Jun 08 '12 at 13:32

1 Answers1

2

I would set Visual Studio to break on all exceptions. In Debug/Exceptions change to this:

enter image description here

This will show you the exact line of code where the problem is occurring.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • it pretty much already tells him the problem anyway, hes expecting a decimal but getting a string, if thats not descriptive enough then man i dunno whats going on with coders lately :L – RhysW Jun 08 '12 at 13:08
  • 1
    He says "The problem is I can't see exactly what is going on, how can I debug or fix this error ?". The implication being that the originating exception is "swallowed" or otherwise obscured by the serialization runtime. My advice would show the offending line of code, not just the exception after the serialization runtime has wrapped it. – Kent Boogaart Jun 08 '12 at 13:27
  • interesting, i might just steal this little tip for myself too ;) – RhysW Jun 08 '12 at 13:29