Deserialize (TargetInvocationException)

  • Thread starter connectpalm03-forum
  • Start date

C

connectpalm03-forum

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(Object
target, SignatureStruct& declaringTypeSig, SerializationInfo info,
StreamingContext context)
at System.RuntimeMethodHandle.SerializationInvoke(Object
target, SignatureStruct declaringTypeSig, SerializationInfo info,
StreamingContext context)
at
System.Reflection.RuntimeConstructorInfo.SerializationInvoke(Object
target, SerializationInfo info, StreamingContext context)
at
System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object
obj, SerializationInfo info, StreamingContext context)
at
System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder
holder)
at System.Runtime.Serialization.ObjectManager.DoFixups()
at
System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler
handler, __BinaryParser serParser, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream
serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top