Friday

c# binary deserialize unable to find assembly version=1.0.0.0, culture=neutral, publickeytoken=null

Got error with binary deserialisation : unable to find assembly xxx version=1.0.0.0, culture=neutral, publickeytoken=null 
It requires to have additional SerializationBinder class .
Here is whole workable storage class:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Runtime.Serialization;
using System.Reflection;

public static  class Storage {

    public const string ConfigurationName = "dev.settings";
    public static string SettingFileName
    {
        get
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ConfigurationName);
        }
    }

    public static  void Save(cProfile gh)
    {
        FileStream writeStream = new FileStream(SettingFileName, FileMode.Create);
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(writeStream, gh);
        writeStream.Close();
    }


    public static cProfile  Load() {
        cProfile ret= new cProfile();

        Manager.getInstance().RunSafe (delegate()
        {
            if (File.Exists(SettingFileName))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Binder = new PreMergeToMergedDeserializationBinder();

                FileStream readStream = new FileStream(SettingFileName, FileMode.Open);
                ret = (cProfile)formatter.Deserialize(readStream);
                readStream.Close();
            }
        });
        return ret;


    }


    sealed class PreMergeToMergedDeserializationBinder : SerializationBinder
    {
        public override Type BindToType(string assemblyName, string typeName)
        {
            Type typeToDeserialize = null;

            // For each assemblyName/typeName that you want to deserialize to
            // a different type, set typeToDeserialize to the desired type.
            String exeAssembly = Assembly.GetExecutingAssembly().FullName;


            // The following line of code returns the type.
            typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
                typeName, exeAssembly));

            return typeToDeserialize;
        }
    }
}





No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...