Question

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??

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top