I have a class named (MyClassA) in ControlClasses.dll and was able to
serialize it to database. Like below
SaveTo(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
}
MyClassA LoadFrom(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
MyClassA obj =
(MyClassA)formatter.Deserialize(stream);
return obj;
}
The above code worked without any problem. Now I have
ControlClasses.dll signed with a StrongKeyName value.
Since then the deserialize no longer works. It keeps throwing an
exception
------------------------------------------------------------------
System.Reflection.TargetInvocationException was caught
Message="Exception has been thrown by the target of an invocation."
Source="mscorlib"
StackTrace:
at System.RuntimeMethodHandle._SerializationInvoke(Ob ject
target, SignatureStruct& declaringTypeSig, SerializationInfo info,
StreamingContext context)
at System.RuntimeMethodHandle.SerializationInvoke(Obj ect
target, SignatureStruct declaringTypeSig, SerializationInfo info,
StreamingContext context)
at
System.Reflection.RuntimeConstructorInfo.Serializa tionInvoke(Object
target, SerializationInfo info, StreamingContext context)
at
System.Runtime.Serialization.ObjectManager.Complet eISerializableObject(Object
obj, SerializationInfo info, StreamingContext context)
at
System.Runtime.Serialization.ObjectManager.FixupSp ecialObject(ObjectHolder
holder)
at System.Runtime.Serialization.ObjectManager.DoFixup s()
at
System.Runtime.Serialization.Formatters.Binary.Obj ectReader.Deserialize(HeaderHandler
handler, __BinaryParser serParser, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Deserialize(Stream
serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Deserialize(Stream
serializationStream)
----------------------------------------------------
If I check the InnerException
{"Could not load file or assembly 'ControlClasses, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)":"ControlClasses,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}
With the inner exception it is clear that it cannot find the Dll which
was not signed, i.e. which didn't had any publickeytoken.
So I have changed the deserialze to say that now use the current
signed dll by added the binder.
MyClassA LoadFrom(MemoryStream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Binder = new FooDeserializationBinder();
MyClassA obj =
(MyClassA)formatter.Deserialize(stream);
return obj;
}
sealed class FooDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string
typeName)
{
return Type.GetType(typeName + ", " +
Assembly.GetExecutingAssembly().FullName);
}
}
But i still get the same exception. Any help, tips, tricks, details
are highly appreciated.
thanks
Vishnu