471,310 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Deserialize (TargetInvocationException)

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
Jun 27 '08 #1
0 2065

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Greg | last post: by
1 post views Thread by Bsiang Tan | last post: by
2 posts views Thread by Thomas S | last post: by
1 post views Thread by tcomer | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.